这是一个合理的方式来'子'JavaScript数组? [英] Is this a reasonable way to 'subclass' a javascript array?

查看:151
本文介绍了这是一个合理的方式来'子'JavaScript数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认识到,严格来说,这不是继承数组类型,而是将这项工作的方式人们所预料的,还是我还是会碰到与。长度之类的一些问题?在那里,如果正常的子类是一个选项,我不会有什么缺点?

 函数向量()
        {
            变种向量= [];
            vector.sum =功能()
            {
                总和= 0.0;
                对于(i = 0; I< this.length;我++)
                {
                    总和+ =这[一]
                }
                返回总和;
            }
            返回向量;
        }        V =矢量();
        v.push(1); v.push(2);
        的console.log(v.sum());


解决方案

我想换一个适当的矢量型里的数组是这样的:

  window.Vector =功能向量(){
  this.data = [];
}Vector.prototype.push =功能推(){
  Array.prototype.push.apply(this.data,参数);
}Vector.prototype.sum = SUM函数(){
  对于(VAR I = 0,S = 0.0,LEN = this.data.length; I< LEN; S + = this.data [我++]);
  返回S;
}VAR向量1 =新的向量();
vector1.push(1); vector1.push(2);
的console.log(vector1.sum());

另外,您可以在阵列构建新的原型功能,然后只用正常的数组。

如果你是一个命名的数组一致的,因此他们都开始用例如或类似小写信息v清楚地标明他们AW向量和不正常的数组,你做的矢量特定原型的功能是相同的,那么它应该相当容易跟踪。

  Array.prototype.vSum =功能VSUM(){
  对于(VAR I = 0,S = 0.0,LEN = this.length; I< LEN; S + =本[我++]);
  返回S;
}变种向量1 = [];
vector1.push(1); vector1.push(2);
的console.log(vector1.vSum());

I realize that, strictly speaking, this is not subclassing the array type, but will this work in the way one might expect, or am I still going to run into some issues with .length and the like? Are there any drawbacks that I would not have if normal subclassing were an option?

        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());

解决方案

I'd wrap an array inside a proper vector type like this:

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());

Alternatively you can build new prototype functions on arrays and then just use normal arrays.

If you are consistent with naming the arrays so they all start with a lowercase v for example or something similar that clearly mark them aw vector and not normal arrays, and you do the same on the vector specific prototype functions, then it should be fairly easy to keep track of.

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());

这篇关于这是一个合理的方式来'子'JavaScript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆