I'm totally new to OOP in Javascript and trying to create a OO quiz app. I came across a great article on OOP in Javascript, but in the examples the guy just makes the instances by hand, like this:
/ A User
firstUser = new User("Richard", "[email protected]");
firstUser.changeEmail("[email protected]");
firstUser.saveScore(15);
firstUser.saveScore(10);
But in my application, of course I want the user to type in his name, email in the html form and when he clicks the button, I want the new instance for that specific user to be created. Now how can I achieve this? I know that I need to set up event listener on the button and the constructor arguments should be equal to the input value that the user will type in. This is what I came with so far, but have no idea if I'm going the right direction:
function User (theName, theEmail) {
/*this.name = {
var theName = document.getElementsByTagName("input").val();
},*/
this.name = function () {
theName = document.getElementById("user_name").val();
}
this.email = function () {
theEmail = document.getElementById("user_email").val();
}
//this.email = theEmail;
this.quizScores = [];
this.currentScore = 0;
}
User.prototype = {
constructor: User,
saveScore: function (theScoreToAdd) {
this.quizScores.push(theScoreToAdd);
},
showNameAndScore: function () {
var Score = this.quizScores.length > 0 ? this.quizScores.join(",") : "No scores yet";
return this.name + "score is " + Score;
},
changeEmail: function (newEmail) {
this.email = newEmail;
return "new email saved:" + this.email;
}
}
function clickBtn () {
var btn = document.getElementById("button");
btn.addEventListener("click", function () {
var user1 = new User (this.name, this.email);
});
}
Still, I feel like this is not the way to do this. Any help would be appreciated, thanks.