【发布时间】:2016-07-05 22:53:48
【问题描述】:
多年来,我一直在编写/扩展我的课程:
function Container(name, contents, logErrors){
this.name = name;
this.contents = contents;
this.logErrors = logErrors || false;
}
function Group(contents){
this.id = new Date().getTime();
Container.call(this, 'Group_'+ this.id, contents);
}
Group.prototype = Object.create(Container.prototype);
Group.constructor = Group;
然而,在某些地方,我看到构造函数属性被分配在子类的原型上,而不是直接在子类上:
function Group(contents){
this.id = new Date().getTime();
Container.call(this, 'Group_'+ this.id, contents);
}
Group.prototype = Object.create(Container.prototype);
Group.prototype.constructor = Group; // <-----
哪个是正确的?
a) Group.prototype.constructor = Group;
b) Group.constructor = Group;
c) both a AND b
d) neither a nor b
如果可能,请引用您的来源。
【问题讨论】:
-
class Group extends Container {}
标签: javascript oop prototype subclassing