English 中文(简体)
交叉浏览到子类 JavaScript 阵列并获得阵列方法的方法?
原标题:Cross-browser way to subclass JavaScript Array and get the array[i] method?

很高兴得知""https://stackoverflow.com/a/10763103/169992"" 这条路 基本上是小分类 JavaScript Array (从链接复制的代码):

function SomeType() {
    this.push(16);
}

SomeType.prototype = [];
SomeType.prototype.constructor = SomeType; // Make sure there are no unexpected results

console.log(new SomeType()); // Displays in console as [16]

但这不是完全的。 是否有办法可以伪造像这个 < em> 和 这样的子类的阵列, 并获得 < code> [ < / code> 方法?

var a = [];
a[3]  = true;
console.log(a.length); //=> 4

var s = new SomeType();
s[3]  = true;
console.log(s.length); //=> 1

这样您仍然可以把它当作一个数组来对待, 当进行循环时 :

for (var i = 0; i < s.length; i++) {
  var item = s[i];
}
最佳回答

只对 (已折旧) 的浏览器有效, 所以它不是交叉浏览器 :

var CustomArray = function ( ) {
  var array = [ 16 ];
  array.__proto__ = this.__proto__;
  return array;
};

CustomArray.prototype = [];
CustomArray.prototype.constructor = CustomArray;

var array = new CustomArray( );
console.log( array instanceof Array );       // true
console.log( array instanceof CustomArray ); // true

array[ 3 ] = 42;
console.log( array.length );                 // 4

我认为没有其他办法了。

问题回答

我发现“ Array” 子类的最佳方法不是“ Array” 子类, 而是另一个“ Array- 类似Object ” 子类, 外面有很多, 一种是使用收藏。 基本上它做着阵列所做的一切( 包括括号记号), 但它是一个“ 习惯” 原型, 这样它很容易被小类地分类, 不像本地原型, 当它们被子类时, 通常会有问题 。

http://codpen.io/dustinpoissant/pen/AXbjxm?editors=0011;rel=“no follow”>http://codpen.io/dustinpoissant/pen/AXbjxm?editors=0011

var MySubArray = function(){
  Collection.apply(this, arguments);
  this.myCustomMethod = function(){
    console.log("The second item is "+this[1]);
  };
};
MySubArray.prototype = Object.create(Collection.prototype);

var msa = new MySubArray("Hello", "World");
msa[2] = "Third Item";
console.log(msa);
msa.myCustomMethod();




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签