Created
April 6, 2015 17:38
-
-
Save WillsonSmith/f34a68e62aaded981b34 to your computer and use it in GitHub Desktop.
do the maths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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