Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript子类化。设置构造函数属性_Javascript_Oop_Prototype_Subclassing - Fatal编程技术网

Javascript子类化。设置构造函数属性

Javascript子类化。设置构造函数属性,javascript,oop,prototype,subclassing,Javascript,Oop,Prototype,Subclassing,多年来,我一直在写/扩展我的课程,就像这样: 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(thi

多年来,我一直在写/扩展我的课程,就像这样:

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) 原因如下

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);
您失去了原来的
组。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
此外,您还可以在此处阅读更多内容:

希望能有所帮助。

您应该始终使用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);
您失去了原来的
组。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
此外,您还可以在此处阅读更多内容:

希望有帮助。

只有a)是正确的。问题是子类的实例(即
)继承指向子类构造函数的
构造函数
属性。它们确实从子类的原型对象(即
Group.prototype
)继承它,而不继承其他内容

如果没有代码使用
.constructor
属性,也可以完全省略该语句。

只有a)是正确的。问题是子类的实例(即
)继承指向子类构造函数的
构造函数
属性。它们确实从子类的原型对象(即
Group.prototype
)继承它,而不继承其他内容


如果没有任何代码使用
.constructor
属性,也可以完全忽略该语句。

类组扩展容器{}
类组扩展容器{}
@Bergi,是否关闭然后再次打开此问题?@Bergi,是否关闭然后再次打开此问题。?