What could be the use of Functions returning function in javascript?
For example refer the below code
function a() {
alert('A!');
return function(){
alert('B!');
};
}
To execute both functions we would call a()();
Is there any practical use for such a code construct. Ignore the alert() that is just for example.
Yes, there is practical use, for example bind
function, which is used to hardly specify context of execution
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
full example:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments