mardi 26 juillet 2016

(Why) does setting `array[NaN]` do nothing?

I was reading an article about javascript PRNGs, and I came across something that surprised me:

var a = new Array();
var b;
a[b++] = 1;

a is now [] and no exception is thrown — the write to the array simply vanishes. Try it out in your browser console if you don’t believe me.

I didn't believe him, so I tried it out in my browser console (Firefox 47):

» var a = new Array();
» var b;
» a[b++] = 1

» a
← Array [  ]
» b
← NaN

There are several curious things going on here, but in particular, I'm trying to understand why the statement a[b++] = 1 doesn't [appear to] do anything.

Aucun commentaire:

Enregistrer un commentaire