javascript:为什么此return语句会导致语法错误? [英] javascript: Why does this return statement cause a syntax error?

查看:76
本文介绍了javascript:为什么此return语句会导致语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apatana 3,我对JS代码格式化程序进行了一些修改,以使其看起来更加清晰,以下是格式化后的代码,它给了我一个错误:

I'm using Apatana 3, i modified the JS code formatter a little bit to let it seem more clear, below is the code after format, it give me an error:

    copyOffset : function( index )
    {
        return
        {
            x : index, y : index
        };
    }

萤火虫给我:

SyntaxError: invalid label

如果我将其更改为:

    copyOffset : function( index )
    {
        return{
            x : index, y : index
        };
    }

会没事的,谁能告诉我这两个return语句之间的区别是什么?

will be OK, Anybody who can tell me what's the diff between these two return statement?

推荐答案

不同之处在于,第一个代码段实际上被解释为...

The difference is that the first snippet is actually interpreted as...

copyOffset : function( index )
{
    return;
    {
        x : index, y : index
    };
}

它被称为自动分号插入:当JavaScript解析器看到一条似乎是完整,但缺少分号,它将尝试修复"它.

It's called Automatic Semicolon Insertion: when JavaScript parser sees a statement that seems to be complete, but misses semicolon, it attempts to 'fix' it.

是的,尽管有时很有帮助,但可能会很烦人.本文详细介绍了此JavaScript功能.

And yes, even though helpful at times, it can be quite annoying. This article explains this JavaScript feature in details.

这篇关于javascript:为什么此return语句会导致语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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