function getB(my1, my2) {
return function() {
a(my1, my2);
}
}
That returned function can be assigned as a variable, and used as an argument to another function without needing to know what was passed to that returned function.
This is known as currying. @Paul_Wilkins explained it well. Here’s an example of what he said:
That screenshot came from this article, which explains currying by example in more detail:
Are you sure? In the example you posted, the function returned from the babyAnimals
function takes an argument (b
) that is passed in later on when the returned function is called. This makes currying possible.
In the OP’s example, the anonymous function being returned doesn’t take any arguments, meaning that partial application is not possible. Instead, I would have said this is a higher-order function. One advantage to this approach is that the returned function would have access to any variables declared within getB()
's scope (via closure), which would not be available from elsewhere in the code.
Please correct me if I’m wrong, as functions returning functions returning functions quickly starts to blow my mind.
I would still call it currying, because it still technically fits the definition. Higher-order functions usually take in a function as an argument. HOFs can still curry a function and used in a way to help encapsulate variables, though.
Currying is a functional. programming construct used more in languages like Clojure. However, you can also use the same methodologies in JavaScript. Functional programming is a whole other topic in itself including the advantages and disadvantages over other methodologies like oop.
But why its returning status is not a function.
var status = function (value){
return function(name){
console.log(name +" your " +value);
}
}
var val= status("Account verified");
val('Brinth');
It is a function, because you are calling the returned function as val('Brinth');
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.