Javascript 子类化。设置构造函数 属性
Javascript Subclassing. Setting the constructor property
多年来我一直 writing/extending 我的 类 就像这样:
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
如果可能,请引用您的来源。
你应该总是使用 a)
这是原因。
function Container(){
// code
}
function Group(){
// code
}
此时观察
console.log(Group.prototype.constructor === Group);
// true
console.log(Group.constructor === Function);
// true
现在如果你这样做
Group.prototype = Object.create(Container.prototype);
您丢失了原来的 Group.prototype
并替换了它的所有方法。这意味着您也失去了原来的 Group.prototype.constructor
。
因此您可能会在此时观察到这一点。
console.log(Group.prototype.constructor === Container);
// true
现在如果你想要类似复制方法的东西。
Group.prototype.copy = function() {
return new this.constructor(this.contents);
};
你可能会得到结果
var group1 = new Group();
console.log(group1.copy() instanceof Group);
// false
这可能出乎意料。
但如果你愿意的话
Group.prototype.constructor = Group;
那么结果如预期
console.log(group1.copy() instanceof Group);
// true
您还可以在这里阅读更多内容:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
希望对您有所帮助。
只有 a) 是正确的。 point of the .constructor
assignment 是子类的实例(即 Group
)继承了指向子类构造函数的 .constructor
属性。他们确实从子类的原型对象(即 Group.prototype
)继承了它,仅此而已。
如果您的代码 none 使用 .constructor
属性.
,您也可以完全省略该语句
多年来我一直 writing/extending 我的 类 就像这样:
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
如果可能,请引用您的来源。
你应该总是使用 a) 这是原因。
function Container(){
// code
}
function Group(){
// code
}
此时观察
console.log(Group.prototype.constructor === Group);
// true
console.log(Group.constructor === Function);
// true
现在如果你这样做
Group.prototype = Object.create(Container.prototype);
您丢失了原来的 Group.prototype
并替换了它的所有方法。这意味着您也失去了原来的 Group.prototype.constructor
。
因此您可能会在此时观察到这一点。
console.log(Group.prototype.constructor === Container);
// true
现在如果你想要类似复制方法的东西。
Group.prototype.copy = function() {
return new this.constructor(this.contents);
};
你可能会得到结果
var group1 = new Group();
console.log(group1.copy() instanceof Group);
// false
这可能出乎意料。
但如果你愿意的话
Group.prototype.constructor = Group;
那么结果如预期
console.log(group1.copy() instanceof Group);
// true
您还可以在这里阅读更多内容:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance
希望对您有所帮助。
只有 a) 是正确的。 point of the .constructor
assignment 是子类的实例(即 Group
)继承了指向子类构造函数的 .constructor
属性。他们确实从子类的原型对象(即 Group.prototype
)继承了它,仅此而已。
如果您的代码 none 使用 .constructor
属性.