Skip to content

Instantly share code, notes, and snippets.

@WillsonSmith
Created April 6, 2015 17:38
Show Gist options
  • Save WillsonSmith/f34a68e62aaded981b34 to your computer and use it in GitHub Desktop.
Save WillsonSmith/f34a68e62aaded981b34 to your computer and use it in GitHub Desktop.
do the maths
(function() {
"use strict";
var calculatorPrototype = {
total: 0,
add: function add(number) {
this.total += number;
return this;
},
subtract: function subtract(number) {
this.total -= number;
return this;
},
multiply: function multiply(number) {
this.total = this.total * number;
return this;
},
pow: function pow(number) {
this.total = Math.pow(this.total, number);
return this;
},
square: function square() {
this.total = this.total * this.total;
return this;
},
divide: function divide(number) {
this.total = this.total / number;
return this;
},
get: function get() {
return this.total;
}
};
function calculator(base, proto) {
var baseNumber = base || 0;
var calcProto = Object.create(proto);
calcProto.total = baseNumber;
return calcProto;
}
var calculate = calculator(10, calculatorPrototype);
calculate.add(5).add(5).divide(2).pow(2).square();
console.log(calculate.get());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment