Skip to content

Instantly share code, notes, and snippets.

@mxdi9i7
Created January 16, 2021 19:55
Show Gist options
  • Save mxdi9i7/051e50b75049f04c797ad9aefdc8b059 to your computer and use it in GitHub Desktop.
Save mxdi9i7/051e50b75049f04c797ad9aefdc8b059 to your computer and use it in GitHub Desktop.
ES6 Function based OOP design
function Counter(startingCount) {
this.startingCount = startingCount;
this.count = this.startingCount;
this.increment = () => {
this.count = this.count + 1;
};
this.decrement = () => {
this.count = this.count - 1;
};
this.reset = () => {
this.count = this.startingCount;
};
this.getCount = () => {
return this.count;
};
}
const my_counter = new Counter(100);
my_counter.increment();
my_counter.increment();
my_counter.increment();
my_counter.decrement();
console.log(my_counter.getCount());
my_counter.reset();
console.log(my_counter.getCount());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment