JavaScript에서이 두 예제의 차이점은 무엇입니까?
전제 조건 :
function SomeBaseClass(){
}
SomeBaseClass.prototype = {
doThis : function(){
},
doThat : function(){
}
}
Object.create를 사용한 상속 예제 A :
function MyClass(){
}
MyClass.prototype = Object.create(SomeBaseClass.prototype);
새 키워드를 사용한 상속 예 B
function MyClass(){
}
MyClass.prototype = new SomeBaseClass();
두 예제 모두 같은 일을하는 것 같습니다. 언제 다른 것을 선택하겠습니까?
추가 질문 : 함수 자체 생성자에 대한 참조가 프로토 타입에 저장되는 아래 링크 (15 행)의 코드를 고려하십시오. 이것이 왜 유용합니까?
https://github.com/mrdoob/three.js/blob/master/src/loaders/ImageLoader.js
발췌 (링크를 열지 않으려는 경우) :
THREE.ImageLoader.prototype = {
constructor: THREE.ImageLoader
}
Object.create
. 이것은 실수이므로 다시 열어야합니다.