주된 이유를 않습니다 데프 만 만 "this.xxx"을 통해 "공공 분야"에 접근 기능 작동하고 Inheriting_FuncDef이 SuperFuncDef의 연장에 대한 지식이 있어야합니다, 다른 현명 "공공 필드는"충돌 일어날 것 :자바 스크립트는이 시간 함수 정의 내에서 상속 정의를 유지하기 위해 노력하고있다 "<strong>프로토를</strong>"를 사용
var G=function(){
var g1state=0;
this.g1=function(){
return g1state++;
}
};
var E = function(){
var e2state=0;
this.e2=function(){
return e2state++;
}
};
E.prototype=new G();
var F= function(){
var f3state=0;
this.f3=function(){
return f3state++;
}
};
F.prototype=new E();
var xx = new F();
var xx2= new F();
console.log("xxg1:___"+xx.g1());//0
console.log("xxg1:___"+xx.g1());//1
console.log("xx2g1:___"+xx2.g1());//2 , need it to be 0, don't wanna share same super() instance/and closure.
console.log("xxe2:___"+xx.e2());//0
console.log("xxe2:___"+xx.e2());//1
console.log("xx2e2:___"+xx2.e2());//2 , need it to be 0;don't wanna share same super() instance/and closure.
console.log("xxf3:___"+xx.f3());//0
console.log("xxf3:___"+xx.f3());//1
console.log("xx2f3:___"+xx2.f3());//0 this f3() is not inherited from super(), and have the expected result.
console.log(xx);
console.log("xx instanceof E:___"+(xx instanceof E));//ture
console.log("xx instanceof F:___"+(xx instanceof F));//true
console.log("xx instanceof G:___"+(xx instanceof G));//ture
"향상된 버전"의 경우 유일한 단점이있는 것 같습니다. "instancof"테스트는 정확할 수 없으며, 그렇지 않으면 사용할 수 있습니다. 그러나 "instancof"부정확성은 주요 단점입니다.
//i test it in ie 11, the result is the same.
var G=function(){
var g1state=0;
this.g1=function(){
return g1state++;
}
};
var E = function(){
Object.setPrototypeOf(this,new G());
var e2state=0;
this.e2=function(){
return e2state++;
}
};
//E.prototype=new G();
var F= function(){
Object.setPrototypeOf(this,new E());
var f3state=0;
this.f3=function(){
return f3state++;
}
};
//F.prototype=new E();
var xx = new F();
var xx2= new F();
console.log("xxg1:___"+xx.g1());//xxg1:___0 ,expected.
console.log("xxg1:___"+xx.g1());//xxg1:___1 ,expected.
console.log("xx2g1:___"+xx2.g1());//xx2g1:___0 ,expected.
console.log("xxe2:___"+xx.e2());//xxe2:___0 ,expected.
console.log("xxe2:___"+xx.e2());//xxe2:___1 ,expected.
console.log("xx2e2:___"+xx2.e2());//xx2e2:___0 ,expected.
console.log("xxf3:___"+xx.f3());//xxf3:___0 ,expected.
console.log("xxf3:___"+xx.f3());//xxf3:___1 ,expected.
console.log("xx2f3:___"+xx2.f3());//xx2f3:___0 ,expected.
console.log(xx);
console.log("xx instanceof E:___"+(xx instanceof E));//xx instanceof E:___false , expect to be true
console.log("xx instanceof F:___"+(xx instanceof F));//xx instanceof F:___false, expect to be true
console.log("xx instanceof G:___"+(xx instanceof G));//xx instanceof G:___true
그래서 어느 방법으로도 완벽한 결과를 얻을 수 없습니다. 그리고 나는 "Funcref.prototype = new superFuncref()"상속 설정은 기본적으로 나를 위해 작동하지 않는다고 생각합니다.
내가 유일한 이유는 Object.setPrototypeOf (this, new SuperFuncRef())입니다. 왜냐하면 나는 모든 "instancof"절을 사실로 만들고 싶기 때문이다. 그렇지 않으면 SuperFuncRef()를 할 것이다. apply (this), 모든 함수를 "this"로 먼저 복사 한 다음 로컬 override를한다. 그러므로 새로운 F()는 단지 F의 인스턴스입니다. 그것은 내가 원한 것이 아닙니다.
감사합니다. 상관 없거나 가치가 없다고 생각한다면 혼자 남겨주세요. down_vote에 더 많은 시간을 낭비하지 마세요. 나는 그 경계에 있습니다. 아니면 아래에 설명하여 영어 문법을 가르쳐 줄 수 있습니다. 당신이 대답을 하든지하지 않더라도, 내가 다시 만족할 때까지 다시 포맷 할 것입니다.
'때문에, 왜 말합니까" "버전을 개선? 'Object.setPrototypeOF()'및'Object.getPrototypeOf'를 사용해야합니다. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Object/proto – laruiss
생각해보십시오 - 생성자 함수 (객체를 만들 때마다) 많은 시간을 호출 할 수 있지만 prototype 속성을 설정하는 작업은 한 번만 수행되므로 객체가 만들어 질 때마다 수행하면 안됩니다. 또한 프로토 타입 속성은 생성자 함수가 호출되기 전에 이미 설정되어 있어야합니다. 그래서 생성자 내부에 설정하면 어떻게됩니까? ... – YK1
哦 ... 매우 합리적입니다. writing_convenience에 관해서는, 왜 그 글에 답하지 않으시겠습니까? 나는 그것을 대답으로 받아 들일 것이다. – Johnny