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);
2 个解决方案
#1
39
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).
接受的答案似乎有点过时了。假设您在相对较新的浏览器上运行,可以在vanilla javascript中使用Function.prototype.bind。或者,如果您使用下划线或jQuery,则可以分别使用_.bind或$ .proxy(如果需要,将回退以调用/应用)。
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)
您可以在这里找到jsfiddle中的代码:http://jsfiddle.net/yMm6t/1/(注意:确保开发人员控制台是打开的,否则您将看不到任何输出)
#2
13
Use Function.prototype.call
to invoke a function and manually set the this
value of that function.
使用Function.prototype.call调用函数并手动设置该函数的this值。
someFunc: function(argument, callback, context) {
callback.call(context); // call the callback and manually set the 'this'
}
Now your callback has the expected this
value.
现在您的回调具有预期的此值。
someFunc('bla-bla', function () {
// now 'this' is what you'd expect
this.anotherFunc();
}, this);
Of course you can pass arguments like normal in the .call
invocation.
当然,您可以在.call调用中传递正常的参数。
callback.call(context, argument);
#1
39
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).
接受的答案似乎有点过时了。假设您在相对较新的浏览器上运行,可以在vanilla javascript中使用Function.prototype.bind。或者,如果您使用下划线或jQuery,则可以分别使用_.bind或$ .proxy(如果需要,将回退以调用/应用)。
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)
您可以在这里找到jsfiddle中的代码:http://jsfiddle.net/yMm6t/1/(注意:确保开发人员控制台是打开的,否则您将看不到任何输出)
#2
13
Use Function.prototype.call
to invoke a function and manually set the this
value of that function.
使用Function.prototype.call调用函数并手动设置该函数的this值。
someFunc: function(argument, callback, context) {
callback.call(context); // call the callback and manually set the 'this'
}
Now your callback has the expected this
value.
现在您的回调具有预期的此值。
someFunc('bla-bla', function () {
// now 'this' is what you'd expect
this.anotherFunc();
}, this);
Of course you can pass arguments like normal in the .call
invocation.
当然,您可以在.call调用中传递正常的参数。
callback.call(context, argument);