Created
December 9, 2014 23:14
-
-
Save rewonc/fee5ebfb2f83e3ba8dac to your computer and use it in GitHub Desktop.
IIFE in loop vs pure functional version
This file contains hidden or 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
// imperative version | |
var i, | |
callLater = {}; | |
for (i = 0; i < 3; i++) { | |
(function (val) { | |
callLater[val] = function () { | |
return "hello " + val; | |
}; | |
}(i)); | |
} | |
console.log(callLater[0]()); // hello 0 | |
console.log(callLater[2]()); // hello 2 | |
// pure functional version | |
var sayHello = function (text) { | |
return ("hello " + text); | |
}; | |
var cacheDefaultFor = function (func) { | |
return function (arg) { | |
return function () { | |
return func(arg); | |
}; | |
}; | |
}; | |
var sayHelloWithValue = cacheDefaultFor(sayHello); | |
console.log(sayHelloWithValue(0)()); // hello 0 | |
console.log(sayHelloWithValue(2)()); // hello 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment