【问题标题】:How to define a new class extending standard javascript class like Array?如何定义一个扩展标准 javascript 类(如 Array)的新类?
【发布时间】:2014-12-23 12:50:35
【问题描述】:

据我了解,定义新 ClassB 扩展 ClassA 的“标准”方式如下:

function ClassA() {
   this.a = {};  // ClassA's instance member.
}
ClassA.prototype.meth1 = function () { .. }  // ClassA's method, shared by all instance.

function ClassB() {}
ClassB.prototype = new ClassA()              // <== "standard" way to extend
ClassB.prototype.meth2 = function () {...}   // ClassB's method

当我尝试定义一个新类 ArrayX 时,如下所示:

function ArrayX() {
}
ArrayX.prototype = new Array()
ArrayX.prototype.removeDup = function removeDup() { 
     var o = [];
     for(var j=0; j<this.length; j++) {
          if(notExist(this[j])
              o.push(this[j])
     }
     return o
     function notExist(itm) {
         for(var j=0; j<o.length; j++) {
               if(o[j]===itm)return false
         }
         return true;  
     }

var x = new ArrayX();
console.log(x.length) // returns 0.  Good
console.log(x)        // returns [].  Good
x[0] = 0;
console.log(x);    // returns [].  No good.  I expect x should have one element.
(new ArrayX([1,1,2,3,3])).removeDup()   // I expect returns [1,2,3]

我知道我可以通过以下方式定义函数removeDup:

Array.prototype.removeDup = function removeDup() { ...}

但是,我只想定义一个新类,扩展一些标准的 javascript 类,例如 Array,甚至是 DOM 类。

那么,如何定义一个像 Array 这样的扩展标准 javascript 类的新类?

【问题讨论】:

  • 基本上,你不应该尝试这样做,它不会很好用。该主题的最终处理位于perfectionkills.com/…。您还可以通过搜索“子类数组 javascript”找到关于 SO 的六个相关问题。顺便说一句,Array 不是“类”,而是“内置类型”。
  • 你能描述一些你将如何使用 ArrayX 的场景吗?我想你只是想创建一些可以应用于 Array 函数的对象?
  • 解决问题不是特定场景。向 Array.prototype 添加新函数是一种方法#1。像这样创建一个新类来扩展 Array ArrayX.prototype = new Array() 是另一种方法#2。那么,为什么方法#2 不起作用?在标准 DOM 类中。它们都从底部的 EventTarget 类扩展到。我只想知道如何进一步扩展这些标准类?我相信一定有什么办法!
  • torazaburo,感谢您的评论和链接 (perfectionkills.com)。从链接中,它有很多内容,我得到了灵感。我已经发布了我的答案,请看一下。 ——

标签: javascript


【解决方案1】:

不需要创建一个自己的类:

Array.prototype.myFunction = function(){
   //your custom code
}

【讨论】:

  • 马克斯,谢谢你的回答。我知道这种方法。现在,我已经改变了问题,以确保在我正在寻找的方向上得到回答。请看一看。
【解决方案2】:

通过引用link#1link#2link#3,我试试这个,它按我的预期工作。

function ArrayX() { 
     // Array.apply(this, arguments)
                                  // calling Array() has same effect as new Array() according to
                                  // [link#3]
                                  // It is no good here.

     this.push.apply(this, arguments); // <-- this works OK. Suggested by link#2

     debugger;                    // Now, `this` has array of value [1, 1, 2, 3, 3], which is
                                  // found in arguments when running new ArrayX(1,1,2,3,3);
                                  // Good.
}
ArrayX.prototype = new Array();   // Suggested by link#1
ArrayX.prototype.removeDup = function removeDup() {
     var o = [];
     for(var j=0; j<this.length; j++) {
          if(notExist(this[j]))
              o.push(this[j])
     }
     return o
     function notExist(itm) {
         for(var j=0; j<o.length; j++) 
              if(o[j]===itm)return false
     }
     return true;  
}
var x = new ArrayX(1,1,2,3,3);  // x is now [1,1,2,3,3].  Good
console.log(x.length)           // 5                      Good
var y = x.removeDup();          // y is [1,2,3].  Good
console.log(y);

【讨论】:

  • length 处理得当吗?
猜你喜欢
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 2018-03-19
  • 2015-11-06
相关资源
最近更新 更多