google-chrome-extension,Javascript,Google Chrome Extension,Scope,Arguments,Anonymous Function,google Chrome Extension" /> google-chrome-extension,Javascript,Google Chrome Extension,Scope,Arguments,Anonymous Function,google Chrome Extension" />

Javascript Can';t将父函数参数传递给子匿名函数

Javascript Can';t将父函数参数传递给子匿名函数,javascript,google-chrome-extension,scope,arguments,anonymous-function,google-chrome-extension,Javascript,Google Chrome Extension,Scope,Arguments,Anonymous Function,google Chrome Extension,在我的chrome扩展的一个后台文件中,我正在检查cookies,但遇到了一个问题。在我的函数chrome.cookies.get({“url”:域,“name”:name},函数(cookie){})我能够获取cookie对象并将其作为变量cookie传递到最后一个参数(匿名函数)中,并使用cookie.value访问其值 问题是我只能从父函数发送返回响应,而不能从chrome.cookies.get调用中发送嵌套匿名函数 在下面的示例中,我能够返回value1的响应(通过另一页上的警报进行验

在我的chrome扩展的一个后台文件中,我正在检查cookies,但遇到了一个问题。在我的函数
chrome.cookies.get({“url”:域,“name”:name},函数(cookie){})我能够获取cookie对象并将其作为变量cookie传递到最后一个参数(匿名函数)中,并使用cookie.value访问其值

问题是我只能从父函数发送返回响应,而不能从
chrome.cookies.get
调用中发送嵌套匿名函数

在下面的示例中,我能够返回value1的响应(通过另一页上的警报进行验证),但是value2永远不会返回。。。在做了大量的阅读之后,我想我把问题缩小到了一个范围错误。。。我的意思是我的响应调用sendResponse(父函数中的一个参数)没有被嵌套的匿名函数访问

function getCookies(request, sender, sendResponse, domain, name) {
    chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
            sendResponse({"myvar":"value2"}); //DOES NOT WORK
            if(cookie){
                anotherFunction(cookie.value);
            }else{
                anotherFunction("0");
            }
    });
    sendResponse({"myvar":"value1"});  // WORKS
}

所以基本上,我只需要找出一些方法,将我的sendResponse参数下推到匿名函数中;或者重新创建不会导致此范围问题的类似方法。在此问题上提供任何帮助或指导都将不胜感激。提前感谢您抽出时间。

嗯。将
sendResponse
强制放入cookie怎么样

function getCookies(request, sender, sendResponse, domain, name) {
  Object.prototype.sendResponse=sendResponse;

  chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
    cookie.sendResponse({"myvar":"value2"});
    if(cookie){
      anotherFunction(cookie.value);
    }else{
      anotherFunction("0");
    }
  });
}

在完全没有测试它的情况下,是否有任何理由不能传递命名回调

function getCookies(request, sender, sendResponse, domain, name) {
  var cb = function(cookie) {
    sendResponse({"myvar":"value2"});
    if(cookie){
      anotherFunction(cookie.value);
    }else{
      anotherFunction("0");
    }
  };
  chrome.cookies.get({"url": domain, "name": name}, cb);
  sendResponse({"myvar":"value1"});  // WORKS
}

调用
cb
时,
sendResponse
仍应在作用域内,否?

关于此问题,您是否在onMessage侦听器的末尾返回true?在我的情况下工作

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { 
  // your getCookies call
  return true;
});
(我也正要问,但我发现了你的问题)

只要这样做:

chrome.cookies.get({"url":"https://...","name":"..."}, function(cookie){
    chrome.tabs.sendMessage(sender.tab.id, {"cookie": cookie.value});
  });

p、 下次尝试在你的问题中添加标记。

我之前尝试过,甚至在添加
var sendResponse=arguments[1]时也尝试过行您建议的值2仍然没有通过sendResponse返回。请查看编辑。我忘了调用回调的方法不会知道第二个参数。让它全球化应该是可行的。您也尝试过删除value1代码吗?我尝试过您的更新版本,但sendResponse仍然没有响应。我喜欢你的想法,尝试使用一个全球性的;你认为这可能与我的chrome扩展的后台脚本有关吗?我相信这意味着它是在扩展本身的上下文中运行的,而不是在窗口页面中运行的。。。此外,为了再次检查代码是否正在实际执行,我定期在send value1行中添加注释<代码>窗口.sendResponse({“myvar”:“value2”})在外部函数的上下文中工作,但仍然不适用于嵌套的匿名函数。是的,我认为后台进程在窗口中可能没有我预期的方式。另一种方法请参见编辑。我尝试过,但仍然没有收到sendResponse的响应。我不确定为什么在
chrome.cookies.get
或更高版本中定义它会有区别。但是我认为范围问题可能是因为函数在cookie调用中;或者是因为函数是匿名的。。。你认为用一个普通的函数替换匿名函数可以工作吗?