1

I want to create a class that would inherit everything from Array class but has its own constructor.

The idea is something like this:

function Array2(value) 
{
    this.push(value*value);
};
Array2.prototype = new Array();
Array2(4);

Obviously this doesn't work because "this.push" doesn't work, because Array2 is not yet based on Array.

2
  • Why do you think this.push doesn't work? Commented May 5, 2015 at 13:53
  • Please use composition over inheritance or else Cthulhu will be coming for you. Commented May 5, 2015 at 13:54

1 Answer 1

4

your code doesn't work because you didn't call it with new keyword which means that this is equal to window.

var a = new Array2(4);
Sign up to request clarification or add additional context in comments.

2 Comments

@Tom, please if this's the answer.Accept it.
It is, but I need to wait 3 more minutes :)

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.