이유는 다음과 같이 구글 폐쇄 도서관에서 goog.inherits
를 않습니다Javascript 상속을 수행 할 때 임시 생성자를 만드는 이유는 무엇입니까?
goog.inherits = function(childCtor, parentCtor) {
function tempCtor() {};
tempCtor.prototype = parentCtor.prototype;
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new tempCtor();
childCtor.prototype.constructor = childCtor;
};
오히려 tempCtor
가 어떤 혜택을 제공합니까
goog.inherits = function(childCtor, parentCtor) {
childCtor.superClass_ = parentCtor.prototype;
childCtor.prototype = new parentCtor();
childCtor.prototype.constructor = childCtor;
};
보다?
둘 다 * childCtor * 인스턴스의 상속 체인에 쓸모없는 객체를 추가합니다. 첫 번째 경우에는 해당 객체가 빈 함수 객체입니다. 두 번째에서는 full * parentCtor * 인스턴스입니다. 따라서 첫 번째가 더 효율적이라고 볼 수 있습니다 (단 하나만 필요하지만 새 빈 함수가 매번 생성되지만). 여기 – RobG
은 흥미로운 읽기입니다. http://bolinfest.com/javascript/inheritance.php – dekdev