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>
JavaScript does not support multiple inheritance, but mixins can be implemented by copying methods into prototype.
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.
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.
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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With