javascript构造函数重置:它是什么?

Sak*_*ake 6 javascript oop

我偶然发现了这张幻灯片: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)

  • 谢谢.Javascript是一种非常奇怪的语言.:) (3认同)
  • 它是.我个人不会使用名称`constructor`来存储构造函数,因为`constructor`在JavaScript中已经有了意义.(只是没有用.) (2认同)