![](/img/trans.png)
[英]How to call async.each in an external function having async.waterfall inside async.each
[英]How to get parent function This inside child function using async.each
var self= this; //parent function context/this
async.each(smpleArray, sampleFunc, function(err){
// if any of the saves produced an error, err would equal that error
});
這是示例函數:
var sampleFunc= function(){
var self = this; //window object
//do something
}
我想在child中獲取父級的這個上下文。 但我在那里得到了windows對象。
我試過了:
async.each(smpleArray, sampleFunc, function(err){
// if any of the saves produced an error, err would equal that error
}.bind(this));
但它不起作用。
如何獲得父母的自我/這個內部子功能?
您必須將上下文綁定到正確的函數,即sampleFunc,如下所示:
sampleFunc.bind(this)
所以你的例子是:
var sampleFunc = function () {
// this is set to parent function context/this
// do something
};
async.each(sampleArray, sampleFunc.bind(this), function (err) {
// if any of the saves produced an error, err would equal that error
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.