Encapsulated function calls
A function which returns a function with encapsulated data , to be used later
by justinwyllie
JavaScript
/*
notice that 'called' is logged when this code is parsed
the parser scans test('help') and just runs it
then test returns a function. this function, the returned one, is not called until it is called
test is not the function which is run by the 'onclick' - it is a factory which returns a function.
the alerted word in this case had to be known at line 17 and is encapsulated in the function which is returned by test
*/
function test(word) {
console.log('called');
return function() {
alert(word);
}
}
a = {
func: test('help')
}
//later on, let's say an onClick
a.func()