Created
July 30, 2015 09:25
-
-
Save ihorkatkov/6618d11306de99e2117a to your computer and use it in GitHub Desktop.
Конструктор Calculator - создаёт расширяемые объекты-калькуляторы.
This file contains 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 Calculator() { | |
var methods = { | |
"-": function(a, b) { | |
return a - b; | |
}, | |
"+": function(a, b) { | |
return a + b; | |
} | |
}; | |
this.calculate = function(str) { | |
var split = str.split(' '), | |
a = +split[0], | |
op = split[1], | |
b = +split[2] | |
if (!methods[op] || isNaN(a) || isNaN(b)) { | |
return NaN; | |
} | |
return methods[op](+a, +b); | |
} | |
this.addMethod = function(name, func) { | |
methods[name] = func; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment