admin管理员组文章数量:1419624
According to Douglas Crockford I could use something like .html (with a little bit of tweaking)... but I am interested in jQuery way of doing it. Is it good practice using $.extend ?
I have 4 classes :
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, i can call every method, property ... from A, B, C and D. Problem es, when I want to test if o is instance of D? How can I overe this problem?
According to Douglas Crockford I could use something like http://javascript.crockford./prototypal.html (with a little bit of tweaking)... but I am interested in jQuery way of doing it. Is it good practice using $.extend ?
I have 4 classes :
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, i can call every method, property ... from A, B, C and D. Problem es, when I want to test if o is instance of D? How can I overe this problem?
Share Improve this question asked Mar 11, 2013 at 15:08 AlFraAlFra 4191 gold badge7 silver badges17 bronze badges 2- Just a note, in practice, in a duck typed language, you rarely care if an object is instanceof something. Why are you checking instanceof D? Note that usually what you really need are en.wikipedia/wiki/Mixin – Benjamin Gruenbaum Commented Mar 11, 2013 at 15:11
-
Multiple inheritance does not work with
instanceof
, since objects can only have a linear prototype chain. – Bergi Commented Mar 11, 2013 at 16:11
2 Answers
Reset to default 4Is it good practice using $.extend
$.extend
is useful for singletons but for prototypes is not ideal.
Using Object.create
(or Crockford's polyfill) you can easily create classes like this. I'm using $.extend
to simply process the properties and give them default values and the module pattern to keep it well organized. Hope this helps:
// Helper that makes inheritance work using 'Object.create'
Function.prototype.inherits = function(parent) {
this.prototype = Object.create(parent.prototype);
};
var Person = (function PersonClass() {
var _defaults = {
name: 'unnamed',
age: 0
};
function Person(props) {
$.extend(this, props, _defaults);
}
Person.prototype = {
say: function() {
return 'My name is '+ this.name;
}
};
return Person;
}());
var Student = (function StudentClass(_super) {
Student.inherits(_super); // inherit prototype
var _defaults = {
grade: 'untested'
};
function Student(props) {
_super.apply(this, arguments); // call parent class
$.extend(this, props, _defaults);
}
Student.prototype.say = function() {
return 'My grade is '+ this.grade;
};
return Student;
}(Person));
var james = new Student({ name: 'James', grade: 5 });
console.log(james instanceof Student); // true
console.log(james instanceof Person); // true
An object has only one prototype, so you cannot make it an instance of two other types with one call.
$.extend(new B(), new D(), ...
creates an object that is an instance of B. Then all properties of D are copied to the newly created object. But the object will still be an instance of B.
Using $.extend
is neither good nor bad per se. But you are bound to jQuery, which makes your code less reusable. And you have to be aware of the fact that $.extend
overwrites properties with the same name, which might or might not be what you want.
本文标签: javascriptHow to implement multiple inheritance using jQuery extendStack Overflow
版权声明:本文标题:javascript - How to implement multiple inheritance using jQuery extend - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745311889a2652971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论