Implement Array-like behavior in JavaScript without using Array

后端 未结 10 1215
暖寄归人
暖寄归人 2020-12-13 07:45

Is there any way to create an array-like object in JavaScript, without using the built-in array? I\'m specifically concerned with behavior like this:

var sup         


        
10条回答
  •  孤城傲影
    2020-12-13 08:12

    Mostly you don't need a predefined index-size for arrays in javascript, you can just do:

    var sup = []; //Shorthand for an empty array
    //sup.length is 0
    sup.push(1); //Adds an item to the array (You don't need to keep track of index-sizes)
    //sup.length is 1
    sup.push(2);
    //sup.length is 2
    sup.push(4);
    //sup.length is 3
    //sup is [1, 2, 4]
    

提交回复
热议问题