let theObj = {
total: 65,
increment: 1
}
// clone object
const cloneObj = function(obj){
return JSON.parse(JSON.stringify(obj));
};
const incrementTotal = function(obj, val){
let newObj = cloneObj(obj);
newObj.increment = val;
return function(){
console.log(newObj.total+" Before increment");
newObj.total = newObj.total + newObj.increment;
console.log(newObj.total+" After increment");
};
};
const incBy2 = incrementTotal(theObj, 2); // it will increment by 2
const incBy10 = incrementTotal(theObj, 10); // it will increment by 10
const incBy25 = incrementTotal(theObj, 25); // it will increment by 25
const incBy125 = incrementTotal(theObj, 25); // it will increment by 125
//and so on
We have cloned the object so every increment will be separated and they will increment as the value is passed. incBy2 will increment by 2, incby10 will increment by 10 and etc.