Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Aren't they just objects that have integer properties; that's why you can

    for (k in ['a', 'b']) {
      console.log(k);
    }
and get back 0, 1? However:

    var a = {0:'a', 1:'b'};
    for (k in a) {
      console.log(k);
    }
also outputs 0, 1.

Edit: turns out, you can even have doubles, too:

    var weird = {3.14:'hello', 6.28:'world'};
    // for loop above emits: 3.14, 6.28
    console.log(weird[3.14]); // emits 'hello'


> Edit: turns out, you can even have doubles, too:

That's because Javascript doesn't actually have integers, it just has "Number":

> The Number type has exactly 18437736874454810627 (that is, 264−253+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic

http://www.ecma-international.org/ecma-262/5.1/#sec-8.5


JavaScript objects can only have string keys (this may have changed in ES2015 with Symbols). If you pass a non-string value in an object literal or inside a []-accessor, that value is converted to a string.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: