Skip to content

Instantly share code, notes, and snippets.

@kavitshah8
Last active March 18, 2017 20:09
Show Gist options
  • Save kavitshah8/34b9f4dad8c6009ab275 to your computer and use it in GitHub Desktop.
Save kavitshah8/34b9f4dad8c6009ab275 to your computer and use it in GitHub Desktop.
Closures
'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]
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