Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript multiple inheritance

can anyone please help with the below code. I am trying to understand multiple inheritance not sure why its not working. BTW below if the code for multiple inheritance. Thanks

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Test Doc</title>
    <script type="text/javascript">
    function classX(){
        this.messageX="this is X Message";
        this.alertX=function(){
            alert(this.messageX);
        };
    }
    function classY(){
        this.messageY="this is Y Message";
        this.alertY=function(){
            alert(this.messageY);
        };
    }
    function classZ(){
        classX.apply(this);
        classY.apply(this);
        this.messageZ="this is Z Message";
        this.alertZ=function(){
            alert(this.messageZ);
        };
    }
    var abjz=new classZ();
    objz.alertZ();
    abjz.alertX();
    </script>
</head>

<body>


</body>
</html>
like image 899
user443946 Avatar asked Sep 10 '11 18:09

user443946


People also ask

Is multiple inheritance allowed in JavaScript?

JavaScript does not support multiple inheritance, but mixins can be implemented by copying methods into prototype.

What is JavaScript mixin?

The definition of mixins can be stated as mixins is a class that contains methods that can be used by other classes without inheriting from that class. The methods in mixin provide certain behavior which is not used alone but can be used to add these behaviors to other classes.

Which inheritance is not supported in JavaScript?

JavaScript supports inheritance if you use it. Look up "JavaScript prototype chain". As for "Why javascript does not support classical inheritance as a default option?" - because that's how JavaScript was defined.

What is multiple inheritance example?

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor.


1 Answers

JavaSript does not have true multiple inheritance. You can inherit from only one prototype and then copy the rest of the properties that you want. You can test this by using the instanceof operator.

After fixing the mis-spellings, your demo works, but in actuality, you are not truly inheriting. To do true JS inheritance:

function A(){}
function B(){}
B.prototype = new A;
b = new B;
console.log(b instanceof A, b instanceof B);
//-> true, true

See also

More about JS inheriance on MDN

Quasi-Multiple Inheritance

function ctorX() {
    this.messageX = "this is X Message";
    this.alertX = function() {
        console.log(this.messageX);
    };
}

function ctorY() {
    this.messageY = "this is Y Message";
    this.alertY = function() {
        console.log(this.messageY);
    };
}

function ctorZ() {
    ctorX.call(this); // This is the quasi-multiple inheritance
    this.messageZ = "this is Z Message";
    this.alertZ = function() {
        console.log(this.messageZ);
    };
}
ctorZ.prototype = new ctorY; // This is the actual inheritance

var objz = new ctorZ();
objz.alertZ();
objz.alertY();
objz.alertX();

console.assert(objz instanceof ctorZ, 'objz is not instance of ctorZ');
console.assert(objz instanceof ctorY, 'objz is not instance of ctorY');
console.assert(objz instanceof ctorX, 'objz is not instance of ctorX');
//The last assert will fail since there is no true multiple inheritance

Demo of Quasi-Multiple Inheritance

Avoid Calling the Super Constructor

HMR brought up the point that in some instances, a user wants to inherit from a particular constructor, but the super-constructor requires parameters and will fail w/o them. The way to bypass this is to create a proxy constructor:

function C(x){if(!x) throw new Error;}
function D(){}
function proxyCtor(){/*should be noop*/}
proxyCtor.prototype = C.prototype;
D.prototype = new proxyCtor;

var d = new D;
console.assert(d instanceof C, 'c is not instance of D');
// will err if incorrect, which it's not

Demo

like image 99
kzh Avatar answered Sep 19 '22 16:09

kzh