Created
May 30, 2019 08:16
-
-
Save raghavgarg1257/97549edd4fd42f1bf46630b248f3dfc4 to your computer and use it in GitHub Desktop.
closure and proto problem
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 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) |
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 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