0

I want to create an inheritance function based on a prototype. I have this JavaScript:

Function.prototype.Inherits = function(parent) {
    this.prototype = new parent();
    this.prototype.constructor = this;
};

function Base() {};

function Foo() {
    this.Inherits(Base);
};

I want a function that does the same as this:

Foo.prototype = new Base();
Foo.constructor = Base();

1 Answer 1

1

Because of the way you call it, this in your Function.prototype.Inherits function would be the object created, not its constructor (Foo). You might want to remove the line this.Inherits(Base); and add this line after (and outside) the Foo declaration: Foo.Inherits(Base);

Sign up to request clarification or add additional context in comments.

2 Comments

@Johan: His point was that doing things with the this is probably not what you want
Wouldnt it help to change Function to Object ?

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.