저는 javascript와 html5를 사용하여 간단한 플랫폼 게임을 작성하고 있습니다. OO 방식으로 자바 스크립트를 사용하고 있습니다. 상속 작업을 얻으려면 다음을 사용합니다.자바 상속 - instanceof 작동하지 않습니까?
// http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
function copyPrototype(descendant, parent) {
var sConstructor = parent.toString();
var aMatch = sConstructor.match(/\s*function (.*)\(/);
if (aMatch != null) { descendant.prototype[aMatch[1]] = parent; }
for (var m in parent.prototype) {
descendant.prototype[m] = parent.prototype[m];
}
};
이 게시물을 위해 다음 예제를 고려하십시오. C는 C & B의 인스턴스 & A. 내가 잘못 때문에
function A() {
this.Name = 'Class A'
}
A.prototype.PrintName = function() {
alert(this.Name);
}
function B() {
this.A();
}
copyPrototype(B, A);
function C() {
this.B();
}
copyPrototype(C, B);
var instC = new C();
if (instC instanceof A)
alert ('horray!');
나는, 나는 horray 경고 상자가 표시 기대 알고있는 것처럼? 아니면 잘못된 방법으로 확인하고 있습니까? 또는 copyPrototype이 instanceof 연산자를 괴롭 히고 있습니까?
항상이 시간을내어 주셔서 감사합니다.
쇼.
안녕하세요 j03m, 당신 말이 맞아요. 지금은 멋진 해결책이 될거예요.하지만이 질문은 꽤 오래되었습니다. , 그리고 그 당시에는 object.create에 대한 광범위한 지원이 없었습니다. 이것은 ECMA 스크립트 5 – Shawson
ah note의 새로운 기능 이었기 때문에 타임 스탬프를 읽었습니다. – j03m
참조 용으로 최신 답변을 갖고있는 것이 좋습니다. :) – Shawson