Implement Array-like behavior in JavaScript without using Array

后端 未结 10 1202
暖寄归人
暖寄归人 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 07:47

    You could also create your own length method like:

    Array.prototype.mylength = function() {
        var result = 0;
        for (var i = 0; i < this.length; i++) {
            if (this[i] !== undefined) {
                result++;
            }
        }
        return result;
    }
    

提交回复
热议问题