Skip to content

Instantly share code, notes, and snippets.

@raghavgarg1257
Created May 30, 2019 08:16
Show Gist options
  • Save raghavgarg1257/97549edd4fd42f1bf46630b248f3dfc4 to your computer and use it in GitHub Desktop.
Save raghavgarg1257/97549edd4fd42f1bf46630b248f3dfc4 to your computer and use it in GitHub Desktop.
closure and proto problem
function calc(value) {
const args = [value];
const recursiveCalc = function(value) {
if (args.length < 2) {
args.push(value);
}
return recursiveCalc;
}
recursiveCalc.__proto__.add = () => args[0] + args[1]
recursiveCalc.__proto__.substract = () => args[0] - args[1]
return recursiveCalc;
}
calc(x)(y).add() //output (x + y)
calc(x)(y).substract() //output (x - y)
function sum(x) {
var plus = x;
var recursiveSum = function(x) {
plus += x;
return recursiveSum;
}
recursiveSum.__proto__.done = function() { return plus; }
return recursiveSum;
}
var s = sum(1)(2)(3)(4)(5)(6);
console.log(s.done())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment