在我正在阅读的教程中,有一部分我不明白:
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号线)
最佳答案
User.constructor
设置User
实例的构造函数。 User.prototype.constructor
设置所有实例的构造函数。因此,如果您设置
User.prototype.constructor = function test(){};
然后
new User().constructor
也将是test
函数。
关于javascript - JS : Is there a difference between class. 原型(prototype).构造函数和类.构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586763/