Wie die Umsetzung Mehrfachvererbung mit Hilfe von jQuery erweitern

Nach Douglas Crockford konnte ich so etwas wie http://javascript.crockford.com/prototypal.html (mit ein wenig Feintuning)... aber ich interessiere mich für jQuery Weg, es zu tun. Ist es gute Praxis mit $.verlängern ?

Habe ich 4 Klassen :

            var A = function(){ } 
            A.prototype = {
                name : "A",
                cl : function(){
                    alert(this.name);
                }
            }
            var D = function(){} 
            D.prototype = {
                say : function(){
                    alert("D");
                }
            }

            var B = function(){} //inherits from A
            B.prototype = $.extend(new A(), {
                name : "B"
            });

            var C = function(){} //inherits from B and D
            C.prototype = $.extend(new B(), new D(), {
                name : "C"
            });


            var o = new C();

            alert((o instanceof B) && (o instanceof A) && (o instanceof C)); //is instance of A, B and C 
            alert(o instanceof D); //but is not instance of D

So, ich kann jede Methode, Eigenschaft, ... aus A, B, C und D. Problem kommt, wenn ich will, um zu testen, ob o ist Instanz von D? Wie kann ich dieses problem lösen?

  • Nur eine Notiz, in der Praxis, in der eine Ente typisierte Sprache, die Sie nur selten darum kümmern, wenn ein Objekt instanceof etwas. Warum sind Sie der überprüfung instanceof-D? Beachten Sie, dass in der Regel, was Sie wirklich brauchen, sind en.wikipedia.org/wiki/Mixin
  • Mehrfache Vererbung funktioniert nicht mit instanceof, da Objekte nur als Prototyp eine lineare Kette.
InformationsquelleAutor AlFra | 2013-03-11
Schreibe einen Kommentar