Last active
August 29, 2015 14:18
-
-
Save WillsonSmith/7340c92f444c3e0893e2 to your computer and use it in GitHub Desktop.
do the more maths from base
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 assign = require("lodash.assign"), | |
mathPrototype = { | |
total: 0, | |
add: function add(number) { | |
this.total += number; | |
return this; | |
}, | |
subtract: function subtract(number) { | |
this.total -= number; | |
return this; | |
}, | |
get: function logTotal() { | |
return this.total; | |
} | |
}, | |
advancedMathPrototype = { | |
pow: function pow(number) { | |
this.total = Math.pow(this.total, number); | |
return this; | |
} | |
}; | |
advancedMathPrototype = assign(Object.create(mathPrototype), advancedMathPrototype); | |
function calculate(base, prototype) { | |
var functions = Object.create(prototype); | |
functions.total = base || 0; | |
return functions; | |
} | |
var calculator = calculate(0, mathPrototype), | |
advancedCalculator = calculate(0, advancedMathPrototype); | |
calculator.add(5).add(5).subtract(5); | |
advancedCalculator.add(5).add(5).pow(2); | |
console.log(calculator.get()); | |
console.log(advancedCalculator.get()); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment