Functions that return functions in javascript

ckv

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.

Vlad Nikitin

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:

http://ejohn.org/apps/learn/#86

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive