有人能解释一下为什么javascript中的这种固有特性不起作用吗?

有人能解释一下为什么javascript中的这种固有特性不起作用吗?,javascript,oop,inheritance,Javascript,Oop,Inheritance,那么为什么子类没有变量呢?我认为这就是原型设计的全部要点。您只是将对超类的引用构造函数分配给子类。prototype,您需要使用new操作符使这个子类。prototype对象成为超类的实例: This is a sub class with name Sub and variable undefined //... SubClass.prototype = new SuperClass(); //.. 您可能希望在上行之后恢复子类.prototype对象的构造函数属性,因为如果不这样做,则使

那么为什么子类没有变量呢?我认为这就是原型设计的全部要点。

您只是将对
超类的引用
构造函数分配给
子类。prototype
,您需要使用
new
操作符使这个
子类。prototype
对象成为
超类的实例:

This is a sub class with name Sub and variable undefined
//...
SubClass.prototype = new SuperClass();
//..
您可能希望在上行之后恢复
子类.prototype
对象的
构造函数
属性,因为如果不这样做,则使用
子类
创建的实例(如示例中的
)将有一个继承的
构造函数
属性错误地指向
超类

This is a sub class with name Sub and variable undefined
//...
SubClass.prototype = new SuperClass();
//..
检查一个例子

推荐文章: