Javascript JS:class.prototype.constructor和class.constructor之间有区别吗
在我正在阅读的一篇教程中,有一部分我不明白:Javascript JS:class.prototype.constructor和class.constructor之间有区别吗,javascript,constructor,prototype,Javascript,Constructor,Prototype,在我正在阅读的一篇教程中,有一部分我不明白: function User (theName, theEmail) { this.name = theName; this.email = theEmail; this.quizScores = []; this.currentScore = 0; } User.prototype = { constructor: User, saveScore:function (theScoreToAdd)
function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = [];
this.currentScore = 0;
}
User.prototype = {
constructor: User,
saveScore:function (theScoreToAdd) {
this.quizScores.push(theScoreToAdd)
},
showNameAndScores:function () {
var scores = this.quizScores.length > 0 ? this.quizScores.join(",") : "No Scores Yet";
return this.name + " Scores: " + scores;
},
changeEmail:function (newEmail) {
this.email = newEmail;
return "New Email Saved: " + this.email;
}
}
我读过这篇文章并浏览了网页,但我不太明白,说:User.constructor和User.prototype.constructor有什么区别吗?
(与第9行类似)用户.constructor设置
用户
实例的构造函数。User.prototype.constructor
设置所有实例的构造函数。因此,如果你设置
User.prototype.constructor = function test(){};
然后
也将是
测试
函数。用户。构造函数设置用户
实例的构造函数。User.prototype.constructor
设置所有实例的构造函数。因此,如果你设置
User.prototype.constructor = function test(){};
然后
也将是测试
功能。执行此操作时
user.prototype={},
实际上,您正在创建新对象。就这样吧
user.prototype=new Object()
并且它的构造函数将更改为Object,所以将构造函数保留为user Object
constructor: User
构造函数是原型的属性。要从类用户调用构造函数,您需要创建类用户的新实例,然后在执行此操作时,只有您可以直接使用构造函数属性
user.prototype={},
实际上,您正在创建新对象。就这样吧
user.prototype=new Object()
并且它的构造函数将更改为Object,所以将构造函数保留为user Object
constructor: User
构造函数是原型的属性。要从类用户调用构造函数,您需要创建类用户的新实例,然后只有您可以直接使用构造函数属性