Javascript 当调用使用回调的函数(在循环中)时,如何传入变量并使其不发生更改?
我在循环中调用sendMessage以获取数组中的一组AppID。调用回调时,我需要检查错误,然后将出现错误的appId“黑名单”。问题是,我尝试的每一种方法都会导致回调中的appId在调用时发生更改!因此,错误的appId被列入黑名单 我试过三个版本(见下文)。一个从未被列入黑名单,另两个做了错误的一个: **这张黑名单是错的**Javascript 当调用使用回调的函数(在循环中)时,如何传入变量并使其不发生更改?,javascript,google-chrome,google-chrome-extension,google-chrome-devtools,google-chrome-app,Javascript,Google Chrome,Google Chrome Extension,Google Chrome Devtools,Google Chrome App,我在循环中调用sendMessage以获取数组中的一组AppID。调用回调时,我需要检查错误,然后将出现错误的appId“黑名单”。问题是,我尝试的每一种方法都会导致回调中的appId在调用时发生更改!因此,错误的appId被列入黑名单 我试过三个版本(见下文)。一个从未被列入黑名单,另两个做了错误的一个: **这张黑名单是错的** for ( var appName in apps ) { var app = apps[ appName ]; var appId = app[
for ( var appName in apps )
{
var app = apps[ appName ];
var appId = app[ "appId" ];
//Send message to that app
chrome.runtime.sendMessage(
app[ "appId" ],
app,
function (response)
{
var lastError = chrome.runtime.lastError;
//Want to blacklist apps here
if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
{
//This blacklists the wrong one!
myGlobalObject.addToTimeout( appId );
}
}
);
}
for ( var appName in apps )
{
var app = apps[ appName ];
var appId = app[ "appId" ];
//Send message to that app
chrome.runtime.sendMessage(
app[ "appId" ],
app,
function (response)
{
var lastError = chrome.runtime.lastError;
//Want to blacklist apps here
if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
{
//This blacklists the wrong one!
myGlobalObject.addToTimeout( this[ "appId" ] );
}
}.bind( app )
);
}
**这张黑名单上也列错了**
for ( var appName in apps )
{
var app = apps[ appName ];
var appId = app[ "appId" ];
//Send message to that app
chrome.runtime.sendMessage(
app[ "appId" ],
app,
function (response)
{
var lastError = chrome.runtime.lastError;
//Want to blacklist apps here
if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
{
//This blacklists the wrong one!
myGlobalObject.addToTimeout( appId );
}
}
);
}
for ( var appName in apps )
{
var app = apps[ appName ];
var appId = app[ "appId" ];
//Send message to that app
chrome.runtime.sendMessage(
app[ "appId" ],
app,
function (response)
{
var lastError = chrome.runtime.lastError;
//Want to blacklist apps here
if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
{
//This blacklists the wrong one!
myGlobalObject.addToTimeout( this[ "appId" ] );
}
}.bind( app )
);
}
**这个从来没有黑名单**
for ( var appName in apps )
{
var app = apps[ appName ];
//Send message to that app
chrome.runtime.sendMessage(
app[ "appId" ],
app,
function (response)
{
var lastError = chrome.runtime.lastError;
//Want to blacklist apps here
if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
{
//Somehow this NEVER blacklists it!
myGlobalObject.addToTimeout( app[ "appId" ] );
}
}
);
}
对于稍后调用的回调,需要在闭包中“冻结”变量的值。有几种方法可以做到这一点,下面是一种使用即时调用函数的方法,该函数将变量作为参数传递,并在该函数闭包中捕获它们,以便它们的值在回调期间保持您想要的值:
for ( var appName in apps )
{
var app = apps[ appName ];
var appId = app[ "appId" ];
// this creates a closure which captures the values of your variables
// and holds them at the desired value until the callback is called
// a separate and unique closure will be created
// for each cycle of the for loop
(function(appID) {
//Send message to that app
chrome.runtime.sendMessage(
appId,
app,
function (response)
{
// since this is called some time later
// variables outside this may have different values
// as the outer for loop continued to run
var lastError = chrome.runtime.lastError;
//Want to blacklist apps here
if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
{
//This blacklists the wrong one!
myGlobalObject.addToTimeout( appId );
}
}
);
})(appID);
}
对于稍后调用的回调,需要在闭包中“冻结”变量的值。有几种方法可以做到这一点,下面是一种使用即时调用函数的方法,该函数将变量作为参数传递,并在该函数闭包中捕获它们,以便它们的值在回调期间保持您想要的值:
for ( var appName in apps )
{
var app = apps[ appName ];
var appId = app[ "appId" ];
// this creates a closure which captures the values of your variables
// and holds them at the desired value until the callback is called
// a separate and unique closure will be created
// for each cycle of the for loop
(function(appID) {
//Send message to that app
chrome.runtime.sendMessage(
appId,
app,
function (response)
{
// since this is called some time later
// variables outside this may have different values
// as the outer for loop continued to run
var lastError = chrome.runtime.lastError;
//Want to blacklist apps here
if ( lastError && lastError.message.indexOf( "not exist" ) !== -1 )
{
//This blacklists the wrong one!
myGlobalObject.addToTimeout( appId );
}
}
);
})(appID);
}
谢谢,这是我一般问题的答案。事实证明,真正的问题只是我定义了一个
函数.prototype.bind
函数,这导致了错误。谢谢,这是我一般问题的答案。事实证明,真正的问题只是我定义了一个函数.prototype.bind
函数,这导致了错误。