The most serious JavaScript WTF I have ever seen

OK, I know it’s late, but stick with me here folks

var x = {a: 1};         //=> {a: 1}
var y = Object.keys(x); //=> ['a']
x[y]                    //=> 1

WTF?

if y = ['a'], x[y] is the same as doing x[['a']], right?

x[['a']];               //=> 1

WTF?

x[[['a']]];             //=> 1

WTF?

x[[[[[[[['a']]]]]]]];   //=> 1

WTF?

Am I just overlooking something simple here. This does not seem like it should work to me.

I’ve been working with JavaScript for >5 years and this is the most serious wtf I’ve ever seen.

Can someone explain what’s happening here? Why does this work?


Source: javascript

Leave a Reply