제 질문은 부모 객체의 프로토 타입 체인을 유지하는 자식 객체에 관한 것입니다.JavaScript의 객체 상속
John Resig의 고급 Javascript 슬라이드 (http://ejohn.org/apps/learn/#76)에서 그는 하위 개체의 프로토 타입 체인을 유지 관리하기 위해 새 부모 개체를 인스턴스화해야한다고 말합니다.
그러나 몇 가지 빠른 테스트를 통해 부모 객체 프로토 타입과 동일한 자식 객체 프로토 타입을 설정함으로써 프로토 타입 체인이 유지된다는 것을 알았습니다.
모든 설명이 크게 도움이 될 것입니다.
원본 코드
function Person(){}
Person.prototype.dance = function(){};
function Ninja(){}
// Achieve similar, but non-inheritable, results
Ninja.prototype = Person.prototype;
Ninja.prototype = { dance: Person.prototype.dance };
assert((new Ninja()) instanceof Person, "Will fail with bad prototype chain.");
// Only this maintains the prototype chain
Ninja.prototype = new Person();
var ninja = new Ninja();
assert(ninja instanceof Ninja, "ninja receives functionality from the Ninja prototype");
assert(ninja instanceof Person, "... and the Person prototype");
assert(ninja instanceof Object, "... and the Object prototype");
존 레식은 그가 처음 Person.prototype
-Ninja.prototype
설정에서 제공하는 코드에서 내 수정 된 버전
function Person(){}
Person.prototype.dance = function(){console.log("Dance")};
function Ninja(){}
// Achieve similar, but non-inheritable, results
Ninja.prototype = Person.prototype;
assert((new Ninja()) instanceof Person, "Will fail with bad prototype chain.");
var ninja = new Ninja();
assert(ninja instanceof Ninja, "ninja receives functionality from the Ninja prototype");
assert(ninja instanceof Person, "... and the Person prototype");
assert(ninja instanceof Object, "... and the Object prototype");
ninja.dance();
[이] (http://stackoverflow.com/questions/5991152/why-do-we-use-boy-prototype-new-human-to-simulate-inheritance)에서 – aaronman
도움이 될 "현대적인"브라우저 :'Ninja.prototype = Object.create (Person.prototype)'. – elclanrs