Your code does not cause inheritance of classX and classY by classZ, it merely copies over their properties/methods.
Object.getOwnPropertyNames(abjz)
reveals:-
messageX,alertX,messageY,alertY,messageZ,alertZ
But for inheritance you want the alert methods to stay only in their respective classes so that any changes to them will be reflected in abjz later. Also, so that there won't be method maintenance issues if you make more instances like abjz.
You could do this:-
var classX = {}
classX.messageX = "this is X Message"
classX.alertX = function(){
alert(this.messageX)
}
var classY = Object.create(classX)
classY.messageY = "this is Y Message"
classY.alertY = function(){
alert(this.messageY)
}
var classZ = Object.create(classY)
classZ.messageZ = "this is Z Message"
classZ.alertZ = function(){
alert(this.messageZ)
}
var abjz = Object.create(classZ)
which is equivalent to:-
function classX(){}
classX.prototype.messageX = "this is X Message"
classX.prototype.alertX = function(){
alert(this.messageX)
}
function classY(){}
classY.prototype = classX.prototype
classY.prototype.messageY = "this is Y Message"
classY.prototype.alertY = function(){
alert(this.messageY)
}
function classZ(){}
classZ.prototype = classY.prototype
classZ.prototype.messageZ = "this is Z Message"
classZ.prototype.alertZ = function(){
alert(this.messageZ)
}
var abjz = new classZ()
Both should output:-
alert( Object.getOwnPropertyNames(abjz) ) //
abjz.alertX() // this is X Message
abjz.alertY() // this is Y Message
abjz.alertZ() // this is Z Message
So now abjz inherits from classZ which inherits from classY which inherits from classX in a linear prototype chain like so:-
abjz --> classZ --> classY --> classX
This is not multiple inheritance though. To get that, abjz needs to inherit directly from each of classX, classY and classZ with no chain, i.e.
abjz --> classZ
abjz --> classY
abjz --> classX
Unfortunately the JS prototype chain system does not permit this, and the flexible benefits of multiple inheritance are killed off by the ordered prototype chain. For example if you also wanted an instance cdef to inherit just from classZ and classX:-
cdef --> classZ --> classX
then in JS the classZ prototype must be changed from classY to classX and that messes up the abjz instance. Whereas in multiple inheritance:-
cdef --> classZ
cdef --> classX
classZ remains nicely unchnaged because there is no awkward chain to deal with.