Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [] // This is an array literal.
- [][123] // This gets item 123 of the array. That value is undefined (=== void 0).
- []['hello'] // This boxes the array type to array object (=== new Array()) and tries
- // to read a property 'hello', from it. Undefined again.
- [][[]] // This treats [] as an accessor. It will try to cast it, if it casts it
- // as number or string doesn't matter, the result is that whatever property
- // it tries to find is undefined.
- [1,2,3] + ' go!' // === '1,2,3 go!'; array is cast to string to allow adding it to a string,
- // becoming [1,2,3].join(',') (and this is why you can console.log() anything,
- // things have a default .toString() implementation).
- [][[]]+[] // This tries to add undefined and empty array. Hmm... can't add those types.
- // So convert them to something add-able.
- // [].join(',') becomes ''. What is the textual conversion of undefined?
- // 'undefined'!
- +[] // Positive number... Array?? Array is not a number, sir. Hm, [].join(',')
- // evaluates to ''. If it was [123].join(','), we could return 123 as the value...
- // But now we have to convert '' to number. An empty string evaluates to false
- // in an if() block, right? And everyone knows false is a bunch of zeros.
- // So +[] is positive zero! (-[] would be === -0, negative zero, but ancient
- // mathematicians would not accept that result).
- 'undefined'[0] // Yay, if we treat the string like an array, it's an array of characters!
- // So this is === 'u'. And that looks like a happy emoticon so it must be a
- // good thing.
- [][[]]+[] // ...so if this is the string 'undefined'...
- +[] // ...and this is zero...
- [][[]]+[][+[]] // ...dang! Syntax error. Operator precedence is not helping...
- ([][[]]+[])[+[]] // ...aha! Parens can tell the right order to apply the operators and therefore
- // how to implicitly cast the types. We can add that character with other
- // characters with +, and make a string! Hooray!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement