Last active
March 18, 2017 20:09
-
-
Save kavitshah8/34b9f4dad8c6009ab275 to your computer and use it in GitHub Desktop.
Closures
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
'use strict'; | |
function mul(x) { | |
return x * x; | |
} | |
function mulWVariance(variance, x) { | |
return x * x + variance; | |
} | |
function mulWVarianceCallBack(variance) { | |
return function(x) { | |
return x * x + variance; | |
}; | |
} | |
console.log([1, 2, 3].map(mul)); // [1, 4, 9] | |
console.log([1, 2, 3].map(mulWVariance.bind(null, 2))); // [3, 6, 11] | |
console.log([1, 2, 3].map(mulWVarianceCallBack(2))); // [3, 6, 11] | |
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 (a, b) { | |
return b ? a + b : b => a + b | |
} | |
console.log( sum(5, 3) ); // 8 | |
console.log( sum(5)(3) ); // 8 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment