Johan
Johan

Reputation: 35194

JavaScript: creating an inheritance function based on a prototype

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();

Upvotes: 0

Views: 213

Answers (1)

Felix Loether
Felix Loether

Reputation: 6190

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);

Upvotes: 1

Related Questions