How are javascript arrays implemented?

后端 未结 9 761
天命终不由人
天命终不由人 2020-12-05 17:09

Namely, how does the following code:

var sup = new Array(5);
sup[0] = \'z3ero\';
sup[1] = \'o3ne\';
sup[4] = \'f3our\';
document.write(sup.length + \"
9条回答
  •  -上瘾入骨i
    2020-12-05 18:09

    A JavaScript array is an object just like any other object, but JavaScript gives it special syntax.

    arr[5] = "yo"
    

    The above is syntactic sugar for

    arr.insert(5,"yo")
    

    which is how you would add stuff to a regular object. It's what is inside the insert method that changes the value of arr.length

    See my implementation of a customArray type here: http://jsfiddle.net/vfm3vkxy/4/

提交回复
热议问题