相关疑难解决方法(0)

数组像Javascript中的对象

我想知道jQuery如何构造其类似数组的对象.我正在尝试解决的关键是它如何设法让控制台将其解释为数组并将其显示为这样.我知道它与长度属性有关,但在玩了一下后我无法弄明白.

我知道这比像对象这样的普通数组没有技术优势,如下例所示.但我认为,当用户进行测试和调试时,这是一个重要的语义元素.

像Object这样的普通数组.

function foo(){
    // Array like objects have a length property and it's properties use integer
    // based sequential key names, e.g. 0,1,2,3,4,5,6 just like an array.
    this.length = 1;
    this[0] = 'hello'
}
// Just to make sure add the length property to the prototype to match the Array 
// prototype
foo.prototype.length = 0;

// Give the Array like object an Array method to test that it works     
foo.prototype.push = Array.prototype.push

// Create an Array like object …
Run Code Online (Sandbox Code Playgroud)

javascript arrays jquery javascript-objects

31
推荐指数
1
解决办法
5389
查看次数

标签 统计

arrays ×1

javascript ×1

javascript-objects ×1

jquery ×1