Isaac
asked on
JavaScript nuance Method question
Hi All,
I am really trying to get an in depth understanding of OOP JavaScript. I found this article with the code below.
My question is about the methods created for the constructor, 'showNameAndScores', 'saveScore' and 'changeEmail'.
Lets look at 'showNameAndScores' as an example.
vs.
I am really trying to get an in depth understanding of OOP JavaScript. I found this article with the code below.
unction 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;
}
}
My question is about the methods created for the constructor, 'showNameAndScores', 'saveScore' and 'changeEmail'.
Lets look at 'showNameAndScores' as an example.
user.prototype.getProperty showNameAndScores = function() //takes a string propertyName. returns that property.
{
var scores = this.quizScores.length > 0 ? this.quizScores.join(",") : "No Scores Yet";
return this.name + " Scores: " + scores;
}
vs.
showNameAndScores:function () {
var scores = this.quizScores.length > 0 ? this.quizScores.join(",") : "No Scores Yet";
return this.name + " Scores: " + scores;
},
What is your question?
ASKER
Oh...sorry.
What's the difference between the two methods I have above?
What's the difference between the two methods I have above?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.