最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: [] = 5, No Syntax Error? Why? - Stack Overflow

programmeradmin0浏览0评论

Yeah, it works in my firebug console. Why does something like this present no syntax error?

[] = 5; [] = doThis(); [] = (function() {})();

Just curious about why it's allowed.

Yeah, it works in my firebug console. Why does something like this present no syntax error?

[] = 5; [] = doThis(); [] = (function() {})();

Just curious about why it's allowed.

Share Improve this question asked Jul 7, 2010 at 1:39 IggyhopperIggyhopper 1576 bronze badges 1
  • It is an error in Chrome btw...I think it's just ignoring your setting and moving on along. – Nick Craver Commented Jul 7, 2010 at 1:43
Add a ment  | 

2 Answers 2

Reset to default 14

Looks like Javascript in FF allows assignment using matching so you can set multiple variables in one shot:

[x,y] = [5,6]; // sets x to 5 and y to 6

"Obviously", some JS implementations are very forgiving, and will silently ignore any extra values, so this does the same:

[x,y] = [5,6,7];

and this would just not assign anything:

[] = [5,6,7];

and on top of that, it looks like FF is even willing to go with a case that doesn't match an array at all:

[x] = 5; // makes x undefined

so if you use [] = whatever then "nothing gets assigned an undefined value", so it boils down to just evaluating the RHS.

(Disclaimer: this is all just guessing the meaning by running stuff in FF...)

I get a "Uncaught ReferenceError: Invalid left-hand side in assignment" in Chrome. IE8 throws "Error: Cannot assign to '[object]'". FF does, indeed, not throw an error.

This is most likely just a failing of the Firefox JavaScript engine, and in any case, does not seem to affect the functionality of the array literal [].

发布评论

评论列表(0)

  1. 暂无评论