← Back to context

Comment by idle_zealot

16 hours ago

Does it count as 0-indexing when your 0 is a floating point number?

Actually in JS array indexing is same as property indexing right? So it's actually looking up the string '0', as in arr['0']

  • Huh. I always thought that JS objects supported string and number keys separately, like lua. Nope!

      [Documents]$ cat test.js
      let testArray = [];
      testArray[0] = "foo";
      testArray["0"] = "bar";
      console.log(testArray[0]);
      console.log(testArray["0"]);
      [Documents]$ jsc test.js
      bar bar
      [Documents]$

    • Lua supports even functions and objects as keys:

        function f1() end
        function f2() end
        local m1 = {}
        local m2 = {}
        local obj = {
            [f1] = 1,
            [f2] = 2,
            [m1] = 3,
            [m2] = 4,
        }
        print(obj[f1], obj[f2], obj[m1], obj[m2], obj[{}])
      

      Functions as keys is handy when implementing a quick pub/sub.

    • They do, but strings that are numbers will be reinterpreted as numbers.

      [edit]

        let testArray = [];
        testArray[0] = "foo";
        testArray["0"] = "bar";
        testArray["00"] = "baz";
        console.log(testArray[0]);
        console.log(testArray["0"]);
        console.log(testArray["00"]);

      1 reply →