这是一种合理的';子类';javascript数组?

这是一种合理的';子类';javascript数组?,javascript,arrays,subclass,Javascript,Arrays,Subclass,我意识到,严格地说,这不是数组类型的子类化,但这是否会以人们期望的方式工作,或者我仍然会遇到一些关于.length之类的问题?如果正常的子类化是一种选择,我会有什么缺点吗 function Vector() { var vector = []; vector.sum = function() { sum = 0.0; for(i

我意识到,严格地说,这不是数组类型的子类化,但这是否会以人们期望的方式工作,或者我仍然会遇到一些关于.length之类的问题?如果正常的子类化是一种选择,我会有什么缺点吗

        function Vector()
        {
            var vector = [];
            vector.sum = function()
            {
                sum = 0.0;
                for(i = 0; i < this.length; i++)
                {
                    sum += this[i];
                }
                return sum;
            }            
            return vector;
        }

        v = Vector();
        v.push(1); v.push(2);
        console.log(v.sum());
函数向量()
{
var向量=[];
vector.sum=函数()
{
总和=0.0;
对于(i=0;i
EDIT——我最初写道,可以像任何其他对象一样对数组进行子类化,这是错误的。每天学习新的东西。这是一个很好的讨论

在这种情况下,作文会更好吗?i、 只需创建一个向量对象,并用数组作为它的后盾。这似乎就是您所处的路径,您只需要向原型添加推送和任何其他方法。

编辑——我最初写道,您可以像任何其他对象一样对数组进行子类化,这是错误的。每天学习新的东西。这是一个很好的讨论


在这种情况下,作文会更好吗?i、 只需创建一个向量对象,并用数组作为它的后盾。这似乎就是您所处的路径,您只需将推送和任何其他方法添加到原型中。

我会将数组包装在适当的向量类型中,如下所示:

window.Vector = function Vector() {
  this.data = [];
}

Vector.prototype.push = function push() {
  Array.prototype.push.apply(this.data, arguments);
}

Vector.prototype.sum = function sum() {
  for(var i = 0, s=0.0, len=this.data.length; i < len; s += this.data[i++]);
  return s;
}

var vector1 = new Vector();
vector1.push(1); vector1.push(2);
console.log(vector1.sum());
window.Vector=函数向量(){
这个.data=[];
}
Vector.prototype.push=函数push(){
Array.prototype.push.apply(this.data,arguments);
}
Vector.prototype.sum=函数sum(){
对于(var i=0,s=0.0,len=this.data.length;i
或者,您可以在数组上构建新的原型函数,然后只使用普通数组

如果你对数组的命名是一致的,因此它们都以小写的v开头,例如,或者类似的东西,清楚地将它们标记为aw vector而不是normal数组,并且你对特定于vector的原型函数也这样做,那么应该很容易跟踪它们

Array.prototype.vSum = function vSum() {
  for(var i = 0, s=0.0, len=this.length; i < len; s += this[i++]);
  return s;
}

var vector1 = [];
vector1.push(1); vector1.push(2);
console.log(vector1.vSum());
Array.prototype.vSum=函数vSum(){
对于(var i=0,s=0.0,len=this.length;i
我将数组包装在一个合适的向量类型中,如下所示:

window.Vector = function Vector() {
  this.data = [];
}

Vector.prototype.push = function push() {
  Array.prototype.push.apply(this.data, arguments);
}

Vector.prototype.sum = function sum() {
  for(var i = 0, s=0.0, len=this.data.length; i < len; s += this.data[i++]);
  return s;
}

var vector1 = new Vector();
vector1.push(1); vector1.push(2);
console.log(vector1.sum());
window.Vector=函数向量(){
这个.data=[];
}
Vector.prototype.push=函数push(){
Array.prototype.push.apply(this.data,arguments);
}
Vector.prototype.sum=函数sum(){
对于(var i=0,s=0.0,len=this.data.length;i
或者,您可以在数组上构建新的原型函数,然后只使用普通数组

如果你对数组的命名是一致的,因此它们都以小写的v开头,例如,或者类似的东西,清楚地将它们标记为aw vector而不是normal数组,并且你对特定于vector的原型函数也这样做,那么应该很容易跟踪它们

Array.prototype.vSum = function vSum() {
  for(var i = 0, s=0.0, len=this.length; i < len; s += this[i++]);
  return s;
}

var vector1 = [];
vector1.push(1); vector1.push(2);
console.log(vector1.vSum());
Array.prototype.vSum=函数vSum(){
对于(var i=0,s=0.0,len=this.length;i
@hvgotcodes答案有一个。我只是想总结一下结论

这似乎是本文中扩展数组的最佳方法

包装纸可以用来。。。其中,对象的原型链被扩充,而不是对象本身

不幸的是,对于我来说,这个方法使用了
arr.\uuuuu proto\uuuuuuu
,在IE 8中不受支持,我必须支持这个浏览器

此方法比上述方法稍慢,但在IE8-中有效

包装器方法避免设置继承或模拟长度/索引关系。相反,类似工厂的函数可以创建普通数组对象,然后直接使用任何自定义方法对其进行扩充。因为返回的对象是数组对象,所以它保持适当的长度/索引关系,以及“Array”的[[Class]]。当然,它也继承了Array.prototype

@答案有一个。我只是想总结一下结论

这似乎是本文中扩展数组的最佳方法

包装纸可以用来。。。其中,对象的原型链被扩充,而不是对象本身

不幸的是,对于我来说,这个方法使用了
arr.\uuuuu proto\uuuuuuu
,在IE 8中不受支持,我必须支持这个浏览器

此方法比上述方法稍慢,但在IE8-中有效

包装器方法避免设置继承或模拟长度/索引关系。相反,类似工厂的函数可以创建普通数组对象,然后直接使用任何自定义方法对其进行扩充。因为返回的对象是数组对象,所以它保持适当的长度/索引关系,以及“Array”的[[Class]]。当然,它也继承了Array.prototype


这只是包装器的另一个例子。玩得开心点。绑定

var _Array = function _Array() {
    if ( !( this instanceof _Array ) ) {
        return new _Array();
    };
};

_Array.prototype.push = function() {
    var apContextBound = Array.prototype.push,
        pushItAgainst = Function.prototype.apply.bind( apContextBound );

    pushItAgainst( this, arguments );
};

_Array.prototype.pushPushItRealGood = function() {
    var apContextBound = Array.prototype.push,
        pushItAgainst = Function.prototype.apply.bind( apContextBound );

    pushItAgainst( this, arguments );
};

_Array.prototype.typeof = (function() { return ( Object.prototype.toString.call( [] ) ); }());

这只是包装器的另一个例子。玩得开心点。绑定

var _Array = function _Array() {
    if ( !( this instanceof _Array ) ) {
        return new _Array();
    };
};

_Array.prototype.push = function() {
    var apContextBound = Array.prototype.push,
        pushItAgainst = Function.prototype.apply.bind( apContextBound );

    pushItAgainst( this, arguments );
};

_Array.prototype.pushPushItRealGood = function() {
    var apContextBound = Array.prototype.push,
        pushItAgainst = Function.prototype.apply.bind( apContextBound );

    pushItAgainst( this, arguments );
};

_Array.prototype.typeof = (function() { return ( Object.prototype.toString.call( [] ) ); }());

有一种方式看起来和感觉上都像是原型继承,但只有一种方式不同

首先,让我们看一看在javascript中实现原型继承的方法之一:

var MyClass = function(bar){
    this.foo = bar;
};

MyClass.prototype.awesomeMethod = function(){
    alert("I'm awesome")
};

// extends MyClass
var MySubClass = function(bar){
    MyClass.call(this, bar); // <- call super constructor
}

// which happens here
MySubClass.prototype = Object.create(MyClass.prototype);  // prototype object with MyClass as its prototype
// allows us to still walk up the prototype chain as expected
Object.defineProperty(MySubClass.prototype, "constructor", {
    enumerable: false,  // this is merely a preference, but worth considering, it won't affect the inheritance aspect
    value: MySubClass
});

// place extended/overridden methods here
MySubClass.prototype.superAwesomeMethod = function(){
    alert("I'm super awesome!");
};

var testInstance = new MySubClass("hello");
alert(testInstance instanceof MyClass); // true
alert(testInstance instanceof MySubClass); // true
var MyClass=函数(条形){
this.foo=bar;
};
MyClass.prototype.awesomeMethod=函数(){
警惕(“我太棒了”)
};
//扩展MyClass
var MySubClass=functi
function SubArray(arrayToInitWith){
  Array.call(this);
  var subArrayInstance = this;
  subArrayInstance.length = arrayToInitWith.length;
  arrayToInitWith.forEach(function(e, i){
    subArrayInstance[i] = e;
  });
}

SubArray.prototype = Object.create(Array.prototype);
SubArray.prototype.specialMethod = function(){alert("baz");};

var subclassedArray = new SubArray(["Some", "old", "values"]);