简体   繁体   English

我如何有效地原型Array或从Array重用代码?

[英]How can i effectively prototype Array or from Array to reuse code?

I built a parser and I would like to 'extend' the Array class in order to use the same functions but have not been successful with: 我构建了一个解析器,我想'扩展'Array类,以便使用相同的函数但是没有成功:

Array.prototype = new Parser()

I would like to create a function that would reuse the shift from the array without me having to add: 我想创建一个函数来重用数组中的转换,而不必添加:

function() { return this.list.shift() }

Also I've been having some problems with the this identity so: 此外,我一直有this身份的一些问题,所以:

How can i effectively prototype Array or from Array to reuse code ? 我如何有效地原型Array或从Array重用代码?

function Parser() {
  this.get_info_colors = function() {
    return {
      'id': self.shift(),
      'name': self.shift(),
      'colors': self.get_colors()
    }
  }
  this.get_info_grad = function() {
    return {
      'id': self.shift(),
      'name': (self.shift() + '_grad'),
      'grad': self.shift()
    }
  }
  this.get_colors = function() {
    this.shift();
    var result = [],
        element;
    while(element != ']') {
      element = this.shift();
      result.push();
    }
    return element;
  }
  this.builder = function(factory) {
    this.shift();
    var result = [],
        element;
    while(element != ']') {
      result.push(factory());
    }
    return result;
  }
  this.color_builder = function() {
    return this.builder(this.get_info_colors);
  }
  this.grad_builder = function() {
    return this.builder(this.get_info_grad);
  }
}

Thanks in advance. 提前致谢。

To extend Array , you need to extend its prototype, like this 要扩展Array ,您需要扩展其原型,就像这样

function CustomArray() {}
CustomArray.prototype = Object.create(Array.prototype);
CustomArray.constructor = CustomArray;

And then you can add all your custom methods on the CustomArray 's prototype, like this 然后你可以在CustomArray的原型上添加所有自定义方法,就像这样

CustomArray.prototype.newMethod = function() {...}

我使用Array.prototype.parser = function(){ // your parser }

First of all, see this post: 首先,看看这篇文章:

Why is extending native objects a bad practice? 为什么扩展本机对象是一种不好的做法?

Second, do you want to extend Parser to derive methods of Array or vice versa? 其次,您是否希望扩展Parser以获取Array的方法,反之亦然? Basically it would be quite okay to have Parser derive methods from Array, but then your prototype assignment is wrong. 基本上,让Parser从Array派生方法是可以的,但是你的原型赋值是错误的。

Parser.prototype = [];

Did you try it that way. 你是这样尝试的吗?

Regarding your issues with this the posted code isn't complete, is it? 关于你提到的问题,与this张贴的代码不完整的,是吗?

EDIT : Some example: 编辑:一些例子:

function Parser() { this.__a = 1; }
Parser.prototype = [];
var a = new Parser;
a.length
// gives 0
a.push( 2 );
// gives 1
a.length
// gives 1
a.__a
// gives 1

EDIT 2 : Example for using constructor as given in comments: 编辑2:使用注释中给出的构造函数的示例:

function Parser() { 
  var args = [].slice.call( arguments );
  args.unshift( 0 ); 
  args.unshift( 0 ); 
  [].splice.apply( this, args ); 
}

Parser.prototype = [];

var c = new Parser( 1, 2, 3 )
c.length
// gives 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM