First of all Hello to everyone, and sorry for my English.
I would still take advantage of the expertise and availability of this community.
In this period I'm studying inheritance in javascript, Crockford patterns, deepcopy shallowCopy etc.
I have also read many post in stakoverflow(here,here ecc..) but i dont hide that my my doubts are still many.
My aim was to create a kind of pseudo-multiple inheritance without using jquery or methods as objectCreate.
Pseudo because, as i have learned from reading various post, multiple inheritance in javascript does not exist.
In my script there are two "superClasses" (HomoErectus an HomoSapiens) an a "subClass" (ModernMan).
My copyProtoDeep function copies all properties of the prototypes of the superclasses in the prototype of the subClass.
This function takes an indefinite number of parameters the first will be the class that inherits from other classes specified in the function call.
To avoid shallowcopy if copyProtoDeep finds an object or an array the function clone this elements.
That said, I would like, if is possible, some answers to the following questions.
Is my code correct?
Is this type of ineritance a mixin?
Which are the negative aspects (or drawbacks) of this procedure?
The use of uber is correct?
Can i improve my code?
I understand that my questions are perhaps too many for a single post, but also a partial response will be read with pleasure.
Thanks to all in advance
<script type="text/javascript">
function copyProtoDeep() {
var len = arguments.length;
arguments[0].prototype.uber=[]
for (j = 1; j <len; j++) {
var parent = arguments[j].prototype;
var child = arguments[0].prototype;
for (var i in parent) {
if (parent.hasOwnProperty(i)) {
if (typeof parent[i] === 'object') {
child[i] = Array.isArray(parent[i]) ? parent[i].slice(0) : JSON.parse(JSON.stringify(parent[i]));
} else {
child[i] = parent[i];
}
}
}
child.uber[j] = arguments[j].prototype
}
}
function HomoErectus(name){
this.name=name
this.sayHello=function(){return 'Hello from '+this.name}
}
HomoErectus.prototype.discovery='fire'
HomoErectus.prototype.scream='yabadabadoo'
HomoErectus.prototype.friends=['wilma','Betty','Barney']
HomoErectus.prototype.uberTest1=function(){
if(this.uber){return 'the ancestor of '+this.name+" discovered "+this.uber[1].discovery}
else{return this.name+' discovered '+this.discovery}
}
function HomoSapiens(name){
this.name=name
this.iam=function(){return 'I am an Homosapiens an my name is '+this.name+' and my weapons are '+this.dangerousWeapons.w1}
}
HomoSapiens.prototype.discovery='wheel'
HomoSapiens.prototype.dangerousWeapons={w1:'bow and arrows',w2:'Spear and shield'}
HomoSapiens.prototype.uberTest2=function(){if(this.uber){return 'yes yes'}else{return 'no no'}}
function ModernMan(){
HomoErectus.apply(this, arguments);
HomoSapiens.apply(this, arguments);
}
copyProtoDeep(ModernMan, HomoErectus,HomoSapiens)
ModernMan.prototype.discovery='pc'
var fred=new HomoErectus('Fred')
console.log(fred.uberTest1())
var bubba=new HomoSapiens('Bubba')
console.log(bubba.uberTest2())
var john = new ModernMan('John')
john.friends.push('Riky')
john.dangerousWeapons.w3='guns and bombs'
console.log(john.uber[1].friends)
console.log(john.uber[2].dangerousWeapons)
console.log(john.uberTest1())
console.log(john.uberTest2())
</script>