Skip to content

Instantly share code, notes, and snippets.

@Rukeeo1
Last active May 6, 2019 11:55
Show Gist options
  • Save Rukeeo1/061485edee007b5a4fb1c83ae4f5aea3 to your computer and use it in GitHub Desktop.
Save Rukeeo1/061485edee007b5a4fb1c83ae4f5aea3 to your computer and use it in GitHub Desktop.
User Constructor Code...this is for my article on object oriented programming
/* a userArray to serve as database for storing Users */
let userArray = [];
/* create an Id variable set it to zero and auto increment it each time the user constructor is called */
let id = 0;
/* A User constructor to create users */
function User(name) {
this.name = name;
accountBalance = this.accountBalance = 0;
userId = this.id = id++;
/* for Everytime the user constructor is called. We push in the created object into UserArray */
userArray.push(this);
}
/* add the makeDeposit method to the prototype */
User.prototype.makeDeposit = function(amount) {
/* make sure the amount is a valid number */
if (typeof amount !== "number") return "please enter a valid number";
/* add the deposited amount to the user's accountBalance */
this.accountBalance += amount;
return this;
};
/* add the withdrawal method to the prototype */
User.prototype.withdrawal = function(amount) {
/* make sure the amount is a valid number */
if (typeof amount !== "number") return "please enter a valid number";
/* deduct the specified amount from the user's accountBalance*/
this.accountBalance -= amount;
return this;
};
module.exports = {
User,
userArray
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment