Geek Speak — HabitableZone

Geek Speak » in reply to Not a knock...

Knock knock

Who's there?

Inheritance in JavaScript!

That's what I meant by JS being a poorly-understood language. The fact is that JS has a remarkably flexible inheritance model unlike any other language (that I know of). There are actually several ways to accomplish inheritance, including multiple inheritance.

Remember that prototype thing? It provides single inheritance:

//constructor function
function Class1()
{
this.foo = 1;
}

function Class2()
{
this.bar = 2;
}

Class2.prototype = new Class1;
Class2 has a foo member as well as bar

Ya want multiple inheritance? You got it:

function Class3()
{
this.base1 = Class1;
this.base1(); // run Class1 constructor
this.base2 = Class2;
this.base2(); // run Class2 constructor
}
Class3 has members foo and bar.

Any questions, class?

Log in or register to post.

The whole thread (16 posts)