譯文:Javascript-function's return

我的理解+google翻譯。若有錯誤,請指正。原文來自MDN:returnexpress

概要

由function返回指定值。ide

版本信息

實現: JavaScript 1.0, NES 2.0(NES:遊戲機在歐洲的發行版本?)
ECMA 版本號: ECMA-262

 

 

語法

return [expression];函數

參數

expression
返回一個表達式,若是省略該表達式,返回undefined。

用例

用例:使用return

下面的函數中,參數x爲數字時返回參數x的平方。ui

function (x){
    return x * x;
}

用例:中斷函數

function會在調用return的地方當即中止運行。google

function counter(){
    for(var count=1;;count++){//無限循環
        document.write(count+"A-");//循環到5的時候
        if(count == 5){
            return
        }
        document.write(count+"B<br>");//循環到4
    }
    document.write(count+"C");//一直都不會發生
}
counter();

輸出spa

1A-1B
2A-2B
3A-3B
4A-4B
5A-

return;return true;return false; return {"變量"};這些都會中斷函數.翻譯

相關參考

Functionscode

相關文章
相關標籤/搜索