JavaScript中“return”语句的奇怪行为 [英] Strange behavior of 'return' statement in JavaScript

查看:123
本文介绍了JavaScript中“return”语句的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript很陌生,刚开始使用JavaScript进行更严肃的开发。我有很多乐趣来实现Module模式。真正让我发疯的一件事是回归陈述的行为。如果你写这是一个很大的区别

I am quite new to JavaScript and just started some more serious development in JavaScript. I had a lot of fun implementing the Module pattern. One thing that really drove me crazy was the behavior of the 'return' statement. It is a big difference if you write

Test = ( function()
{
    var message = "Hello World!";

    return
    {
        // Does not work
        printTest: function() { window.alert(message); }
    };
}());

Test = ( function()
{
    var message = "Hello World!";

    return {
        // Works well
        printTest: function() { window.alert(message); }
    };
}());

注意'return'语句后的大括号。

Note the curly brace after the 'return' statement.

这是一个典型的愚蠢新手错误并且在某处有详细记录吗?

Is that a typical stupid rookie error and is well documented somewhere?

Firebug无法给出提示。 IE9和Chrome确实在代码的后面位置报告了一些模糊的语法错误: printTest:function中的 function 语句后面的左括号()

Firebug was not able to give a hint. IE9 and Chrome did report some obscure syntax error at a later location in the code: the opening brace after the 'function' statement in "printTest: function()".

对此有何评论? JavaScript中有更多这样的陷阱吗?

Any comments on this? Are there more such pitfalls in JavaScript?

推荐答案

如果你把括号放到下一行,解释器会认为有一个分号。

If you put your brackets to the next line, the interpreter assumes that there is a semi-colon.

因此,您的退货声明将被解释为:

So your return statement will be interpreted as:

return; 
{
   printTest: function() { window.alert(message); };
}

如果我记得很清楚,我会在中解决这个问题JavaScript:好的部分

If I remember well, i've red about this problem in JavaScript: The Good Parts


JavaScript有一种机制,试图通过自动插入分号来纠正错误的程序。
不依赖于此。它可以掩盖更严重的错误。
有时会在不受欢迎的地方插入分号。考虑返回语句中
分号插入的后果。如果return语句返回一个值,那么表达式
必须在与返回相同的行上开始:

JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons. Do not depend on this. It can mask more serious errors. It sometimes inserts semicolons in places where they are not welcome. Consider the consequences of semicolon insertion on the return statement. If a return statement returns a value, that value expression must begin on the same line as the return:



return
{
   status: true
};




这似乎返回一个包含状态成员的对象。不幸的是,分号插入将
转换为返回undefined的语句。没有任何警告说分号插入导致
误解了程序。如果 {位于上一行
的末尾而不是下一行的开头,则可以避免此问题:

This appears to return an object containing a status member. Unfortunately, semicolon insertion turns it into a statement that returns undefined. There is no warning that semicolon insertion caused the misinterpretation of the program. The problem can be avoided if the { is placed at the end of the previous line and not at the beginning of the next line:



return {
   status: true
};

JavaScript:道格拉斯克罗克福德的好零件。版权所有2008 Yahoo! Inc.,
978-0-596-51774-8。

这篇关于JavaScript中“return”语句的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆