Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass context to anonymous function?

There are some function, thats do something long work and its provides callback.

someFunc: function(argument, callback, context) {   // do something long    // call callback function   callback(context); } 

In application I use this function

someFunc('bla-bla', function (context) {   // do something with this scope   context.anotherFunc(); }, this); 

How to implement callback function without passing context parameter?

Need some like this:

someFunc('bla-bla', function () {   // do something with this scope   this.anotherFunc(); }, this); 
like image 351
acelot Avatar asked Nov 08 '12 01:11

acelot


People also ask

Can you pass parameters to anonymous function JavaScript?

The syntax is simple: you can simply declare the anonymous function and make it execute by just calling it using the parenthesis at the end of the function. You can simply pass the parameters inside the immediate execution of the anonymous function as we have seen in the above example.

How do we assign an anonymous function to a variable?

We write the anonymous function in Javascript by writing function() followed by the parentheses, where we may pass the parameters, and skipping the function name. Then in a pair of curly braces {}, we write our desired code. Finally, we assign this function to a variable.

Can you pass anonymous function as an argument to another function?

Summary. Anonymous functions are functions without names. Anonymous functions can be used as an argument to other functions or as an immediately invoked function execution.

Can you use an anonymous function in a callback?

console. log(Arr. map(function(x){ return myCallback(x+2)})); In JavaScript, callbacks and anonymous functions can be used interchangeably.


1 Answers

The accepted answer seems somewhat outdated. Assuming you're operating on a relatively modern browser, you can use Function.prototype.bind in vanilla javascript. Alternatively, if you are using underscore or jQuery, you can use _.bind or $.proxy respectively (which will fallback to call/apply if necessary).

Here is a simple demonstration of these three options:

// simple function that takes another function // as its parameter and then executes it. function execute_param(func) {     func(); }  // dummy object. providing an alternative context. obj = {}; obj.data = 10;  // no context provided // outputs 'Window' execute_param(function(){     console.log(this); });  // context provided by js - Function.prototype.bind // src: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind // outputs 'Object { data=10 }'' execute_param(function(){     console.log(this); }.bind(obj));  // context provided by underscore - _.bind // src: http://underscorejs.org/#bind // outputs 'Object { data=10 }' execute_param(_.bind(function(){     console.log(this); },obj));  // context provided by jQuery - $.proxy // src: http://api.jquery.com/jQuery.proxy/ // outputs 'Object { data=10 }' execute_param($.proxy(function(){     console.log(this); },obj)); 

You can find the code in a jsfiddle here: http://jsfiddle.net/yMm6t/1/ (note: ensure that the developer console is open, or you won't see any output)

like image 120
EleventyOne Avatar answered Sep 23 '22 02:09

EleventyOne