gpt4 book ai didi

javascript - JS : Is there a difference between class. 原型(prototype).构造函数和类.构造函数

转载 作者:行者123 更新时间:2023-12-02 14:34:11 24 4
gpt4 key购买 nike

在我正在阅读的教程中,有一部分我不明白:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com