in my perspective, the const
definition clearly indicates that it triggers a function stored in an object
. As you mentioned, this can be simplified to:
const invoke = (method) => {
return (object) => {
return object[method] ;
}
}
I believe the purpose of this is to make the code more readable and concise: to invoke
the function a
from the functions
object ( functional-programming
).
Reading this article on functional programming
Functional programming is declarative rather than imperative, and
application state flows through pure functions. Contrast with object
oriented programming, where application state is usually shared and
colocated with methods in objects.
However, the term invoke
made me consider Immediately invoked functions. Therefore, the usage of const invoke
can be:
Retrieve a function from the object (without executing it) to avoid instantiating the entire object, store the function in a variable, and potentially manipulate its prototype
.
Call the function (with parentheses).
Access a property from an object.
Immediately invoke a function within an object.
const myFns = {
'a' : function(x){
console.log(x || 'something to log when no params passed');
},
'b': {
username : 'Doe'
}
}
const invoke = method => object => object[method]
let myFunction = invoke('a')(myFns);
myFunction('hello from myFunction'); // call it or modify myFunction.prototype ... etc.
invoke('a')(myFns)('hello'); // simply call it
let user = invoke('b')(myFns); // get a property
console.log(user.username);
(invoke('a')(myFns))(); // immidiatly invoke the function
Possibly to avoid eval() :P