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.

3 Answers 3

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() {...}
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

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

8 Comments

Its not they started when I did Parser.prototype = [] the this turned into window.
Using this is very special ... this is a special local variable in context of your function, only, to be declared on invoking.
If I do Parser.prototype. Then how can I turn [1,2,3,4] into new Parser(1,2,3,4).
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.
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.