javascript:为什么这个return语句会导致语法错误?(javascript: Why does this return statement cause a syntax error?)

编程入门 行业动态 更新时间:2024-10-26 18:28:06
javascript:为什么这个return语句会导致语法错误?(javascript: Why does this return statement cause a syntax error?)

我使用的是Apatana 3,我修改了JS代码格式化程序,让它看起来更清晰,下面是格式化后的代码,它给我一个错误:

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

萤火虫给我:

SyntaxError: invalid label

如果我将其更改为:

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

没问题,任何人都可以告诉我这两个回报声明有什么区别?

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 }; }

firebug give me:

SyntaxError: invalid label

if i change it to:

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

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

最满意答案

不同的是,第一个片段实际上被解释为...

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

它被称为自动分号插入 :当JavaScript解析器看到似乎完整的语句,但错过了分号时,它会尝试“修复”它。

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

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

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

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.

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

更多推荐

本文发布于:2023-07-05 07:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   语法错误   return   javascript   syntax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!