我偶然发现了这张幻灯片:http://www.slideshare.net/stoyan/javascript-patterns#postComment
在第35页:
选项5 +超级+构造函数重置
function inherit(C, P) {
var F = function(){};
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C; // WHY ???
}
Run Code Online (Sandbox Code Playgroud)
我不明白.任何人都可以解释一下最后一行是什么?
C.prototype.constructor = C; // WHY ???
Run Code Online (Sandbox Code Playgroud)
谢谢
Jon*_*han 11
这给出了http://phrogz.net/JS/Classes/OOPinJS2.html的解释
特别是
Cat.prototype = new Mammal(); // Here's where the inheritance occurs
Cat.prototype.constructor=Cat; // Otherwise instances of Cat would have a constructor of Mammal
Run Code Online (Sandbox Code Playgroud)