1

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.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:

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

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.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
user1634074
  • 681
  • 8
  • 17

3 Answers3

1

To extend Array, you need to extend its prototype, like this

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.prototype.newMethod = function() {...}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • While this adds the array methods to the custom object, it doesn't make it behave like an array. E.g. using property assignment like `obj[3] = 42;` won't increase the length of the "custom array". Just wanted to make that clear. – Felix Kling Apr 14 '14 at 15:42
1

I use Array.prototype.parser = function(){ // your parser }

Fuzzyma
  • 7,619
  • 6
  • 28
  • 60
1

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? Basically it would be quite okay to have Parser derive methods from Array, but then your prototype assignment is wrong.

Parser.prototype = [];

Did you try it that way.

Regarding your issues with this the posted code isn't complete, is it?

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:

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
Community
  • 1
  • 1
Thomas Urban
  • 4,649
  • 26
  • 32
  • Its not they started when I did `Parser.prototype = []` the `this` turned into window. – user1634074 Apr 14 '14 at 14:27
  • Using `this` is very special ... `this` is a special local variable in context of your function, only, to be declared on invoking. – Thomas Urban Apr 14 '14 at 14:29
  • If I do Parser.prototype. Then how can I turn [1,2,3,4] into new Parser(1,2,3,4). – user1634074 Apr 14 '14 at 14:33
  • Your "constructor" might use `arguments` to push all its elements into array using push() ... ain't performant, but sufficient for this sort of syntactical sugar. – Thomas Urban Apr 14 '14 at 14:35
  • Or try using `this.splice( 0, 0, arguments )` in constructor. – Thomas Urban Apr 14 '14 at 14:36
  • I doesn't work the functions from Array are not in the Parser object just the `length` :( – user1634074 Apr 14 '14 at 15:18
  • 1
    Tried the second example above in Google Chrome before providing here. Tried it with NodeJS right this moment again. It was working in both situations and this time I was able to append `c.shift();` four times giving me `1`, `2`, `3` and `undefined` ... so it is working. What context are you working in? What is your engine/browser for testing this? – Thomas Urban Apr 15 '14 at 09:04
  • Your right it does work. For some reason it wasn't. Sorry about that. – user1634074 Apr 16 '14 at 14:21