I'm trying to train myself to write chaining function but got error of
Cannot read property 'minus' of undefined(…)
What's wrong with my code?
var math = function(){
var result = 0;
var add = function(param){
result += param;
};
var minus = function(param){
result -= param;
};
var print = function(){
console.log(result)
};
return {add:add, minus: minus, print:print};
}
var calculator = math();
var result = calculator.add(5).minus(1).print();
console.log(result)
calculator.add(5)will return youundefinedwhich, of course, does not have a.minus.