reusable arithmatic class
by Chetan Hanumantha
JavaScript
class Arithmatic
{
constructor(a,b, artihmaticFunction){
this.a = a;
this.b = b;
this.fn = artihmaticFunction
}
call () {
console.log(this.a+' , '+this.b);
return this.fn(this.a, this.b)
}
}
/*Arithmatic.prototype.call = function(a ,b, callbackFn) {
console.log(' test '+this.a);
var context = {
a: a,
b: b,
fn: callbackFn
};
return this.callbackFn.call(a ,b);
}*/
// ------
var calc = new Arithmatic();
var sum = calc.call(2,3, function(a, b){
return a+b;
});
var product = calc.call(2,3, function(a, b){
return a*b;
});