开发者

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

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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜