Reading the OP and Nick's comments , I think I can expand Adrian's answer a bit to make it clearer.
Perfectly valid JavaScript.
JavaScript treats object object names as strings, objects cannot contain other types or other objects as keys, they are just strings.
The bracket designation of the accessor property ( MemberExpression [ Expression ] ) implicitly converts the expression between the brackets to a string, therefore:
var obj = {}; obj[{}] = "foo"; alert(obj["[object Object]"]);
In the above example, you can see that I am assigning a value to the {} property, and {}.toString() (or {}+'' ) creates the string "[object Object] (via Object.prototype.toString ).
Expression 1 [{}] implicitly converts the primitive 1 Number to an object (this is done by the property accessory), and it searches for a property with the name "[object Object]" , the search for properties is performed on Number.prototype and Object.prototype , for example:
1['toString'] === Number.prototype.toString;
Finally, the expression 1 [{}] itself enclosed in brackets ( [1 [{}]] ), it is actually an Array literal.
In conclusion, here is how the analyzer evaluates the expression:
[1 [{}]]; // ^ The accessor expression is evaluated and converted to string [1 ["[object Object]"]]; // ^ A property lookup is made on an Number object // trying to access a property named "[object Object]" [undefined]; // ^ the property is obviously not found [undefined]; //^ ^ // An array literal is created with an element `0` which its value is `undefined`
CMS
source share