译文: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

相关文章
相关标签/搜索