더글러스 크록 포드 (Douglas Crockford)에 따르면 http://javascript.crockford.com/prototypal.html과 같은 것을 사용할 수 있습니다. (조금 조정할 수 있습니다.)하지만 jQuery 방식에 관심이 있습니다. $ .extend를 사용하는 것이 좋은 연습일까요? , 내가 모든 메소드를 호출 할 수 있습니다, 속성을 그래서jQuery를 사용하여 다중 상속을 구현하는 방법
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
... I는 O의 인스턴스의 경우는 테스트 할 때 A가, B, C 및 D 문제가 온다에서 :
나는 4 개 수업을 디? 이 문제를 어떻게 극복 할 수 있습니까?
사실 오리 유형의 언어로 작성된 참고 만해도 객체가 instanceof인지는 거의 신경 쓰지 않습니다. 왜 instanceof D 검사를하고 있니? 대개 정말로 필요한 것은 http://en.wikipedia.org/wiki/Mixin입니다. –
객체는 선형 프로토 타입 체인 만 가질 수 있기 때문에 다중 상속은'instanceof'와 함께 작동하지 않습니다. – Bergi