Javascript 为什么chrome.debugger.attach回调函数应该像cb.bind(null,…)一样使用

Javascript 为什么chrome.debugger.attach回调函数应该像cb.bind(null,…)一样使用,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我正在开发一个chrome扩展。现在我遇到了chrome.debugger的一个问题,调试器只能附加一个http请求。无法检测到选项卡的rest请求。我发现这是由回调函数引起的 根据,当第三个参数是回调函数时,使用chrome.debugger.attach。在live headers中,附加代码如下: chrome.browserAction.onClicked.addListener(function(tab) { chrome.debugger.attach({tabId:tab.

我正在开发一个chrome扩展。现在我遇到了chrome.debugger的一个问题,调试器只能附加一个http请求。无法检测到选项卡的rest请求。我发现这是由回调函数引起的

根据,当第三个参数是回调函数时,使用
chrome.debugger.attach
。在live headers中,附加代码如下:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.debugger.attach({tabId:tab.id}, version,
        onAttach.bind(null, tab.id));
});

var version = "1.0";

function onAttach(tabId) {
    if (chrome.runtime.lastError) {
        alert(chrome.runtime.lastError.message);
        return;
    }

    chrome.windows.create(
        {url: "headers.html?" + tabId, type: "popup", width: 800, height: 600});
}
在我的代码中,我这样调用attach(只是不使用bind):

当我使用前一种格式时,一切正常。我不明白两者的区别和结果

问题:当我使用第二种方法时,将不再有
网络。已收到响应
网络。将收到请求
消息。我已尝试使用并发现选项卡的调试器附件已分离。当我返回到第一个方法时,一切都正常

对不起,我的英语很差


感谢您的帮助

您可能忽略了
bind
函数的功能(以及为什么它在这种情况下会产生如此大的差异)。如果对任何与WebDev相关的内容有疑问,您可以前往

关于:

创建一个新函数,该函数在调用时将其“this”关键字设置为提供的值,并在调用新函数时在任何提供的参数之前具有给定的参数序列


那么,你的方式和医生的方式有什么区别呢

你的方式:

function myAttach() {...}   <--  A function that expects no arguments
...
chrome.debugger.attach(
    ..., 
    myAttach   <-- 'myAttach()' is called, with no arguments
);                 and therefore no way to refer to the tab's id
function onAttach(tabId) {...}   <-- A function that expects 1 argument
...
chrome.debugger.attach(
    ..., 
    onAttach.bind(null, tab.id)   <-- Creates a new function, let's name it "tmpFunc".
);                                    'tmpFunc()' is called, with no arguments

函数myAttach(){…}我不明白这个问题。。。(当你做了什么,应该做什么,问题出在哪里?)顺便说一句,你的第二次“尝试”甚至是无效的。您应该会在控制台日志中看到错误。对不起,我的英语很差。问题是,当我用callbacksorry表示我的英语很差时。问题是,当我在没有
cb.bind(null,args)
的情况下使用回调函数时,调试器可以只附加该选项卡一次,当访问该选项卡中的其他链接时,调试器无效,没有
网络。将接收到响应的
网络。requestWillBeSent
@专家系统
function onAttach(tabId) {...}   <-- A function that expects 1 argument
...
chrome.debugger.attach(
    ..., 
    onAttach.bind(null, tab.id)   <-- Creates a new function, let's name it "tmpFunc".
);                                    'tmpFunc()' is called, with no arguments