denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.99k stars 2.55k forks source link

Accessing objects with arrays #11

Closed namirsab closed 7 years ago

namirsab commented 7 years ago

I find this one really funny as well:

var obj = { prop: 3 }
obj[['prop']] // -> 3

Of course the explanation is easy, the bracket operator converts everything you pass to a string, and converting an 1 element array to string is just returning the conversion of that element to string.

zswang commented 7 years ago

Multidimensional array

var map = {};
var x = 1;
var y = 2;
var z = 3;

map[[x, y, z]] = true;
map[[x + 10, y, z]] = true;

map["1,2,3"] // -> true
map["11,2,3"] // -> true
denysdovhan commented 7 years ago

oh god

guys, can you please send corresponding PRs?

namirsab commented 7 years ago

PR created:

39