1

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>
Community
  • 1
  • 1
Devima
  • 1,566
  • 2
  • 10
  • 16
  • 1
    why javascripters hate composition so much they resort to cheap tricks like this ? With this kind of helper , one has little control over what gets overwritten or not. Let's say one wants HomoErectus friends with HomoSapiens discovery ... guess what , the helper doesnt help,at all. Strategies would fix that mess. – mpm Mar 27 '14 at 09:53
  • possible duplicate of [Multiple inheritance/prototypes in JavaScript](http://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes-in-javascript) – kol Mar 27 '14 at 10:22

1 Answers1

0

there are my answers:

Is my code correct?

I think the mixin has no exact rules, so your code is probably right. I didn't find any bigger problem.

Is this type of ineritance a mixin?

If you inherit all methods, then the mixin is (can be) equal to multiple inheritance. And because you also called super constructor, it looks more like inheritance than mixin.

Which are the negative aspects (or drawbacks) of this procedure?

The diamond problem. You probably heard about it. Its a common problem of multiple inheritance. If HomoSapiens and HomoErectus will have same method, then according to your mixin last argument of your copyProtoDeep wins, so HomoSapiens method will be used. But it is the thing about mixins... with inheritance, it is known problem and inheritance isnt supposed to solve it. On the other side, every solution from mixin is acceptable. So if you define that last argument method will have a priority, then it is a correct solution.

The use of uber is correct?

I see one problem. What if parent is also a mixin? Then it has parent.prototype.uber, which will overwrite child.prototype.uber.

Can i improve my code?

I would try to fix the previous problem with uber. For example watch ecma-262 5.1 specification of Property Attributes. This is good thing for libs or patterns. With it you can implement uber different way, so it can't be overwritten for example.

Last words

I have to say that Im not a mixin or multiple inheritance expert. I avoid it. Im just good in javascript and rather use simple inheritance and interfaces. So this was just my point of view.

Entity Black
  • 3,401
  • 2
  • 23
  • 38
  • Although with some delay, thank you for your interest. I thought I had already voted your answer but I was wrong. I vote now and thanks. – Devima Mar 29 '14 at 15:36