简体   繁体   English

如何修复函数在JS中未定义

[英]How to fix function is undefined in JS

Porting a Chrome extension to Firefox. 将Chrome扩展程序移植到Firefox。

// Changes extension icon on tab switch
browser.tabs.onActivated.addListener(async function (change) {
  var status = await currentStatus();
  browser.tabs.get(info.tabId, function (change) {
    if (!(status) || info.url.match(/https:\/\/url.com\/*/) == null) {
      browser.browserAction.setIcon({
        path: 'images/icon-19-disabled.png'
      });
    } else {
      browser.browserAction.setIcon({
        path: 'images/icon-19.png'
      });
    }
  });
});

// Changes extension icon on tab URL switch
browser.tabs.onUpdated.addListener(async function (change, tab) {
  var status = await currentStatus();
  if (tab.url == undefined) {
    return;
  }

  if (!(status) || tab.url.match(/https:\/\/url.com\/*/) == null) {
    browser.browserAction.setIcon({
      path: 'images/icon-19-disabled.png'
    });
  } else {
    browser.browserAction.setIcon({
      path: 'images/icon-19.png'
    });
  }
});

Debugger is spitting out 'change is undefined' 调试器吐出“未定义更改”

I've tried changing the function name to info, but the debugger throws out the same error. 我尝试将函数名称更改为info,但是调试器抛出相同的错误。

Well if your js parser is telling you change is undefined 好吧,如果您的js解析器告诉您change is undefined

then thats exactly the problem. 那就是问题所在。 change is undefined. change未定义。 renaming it to info doesn't change a thing, unless you also have an object info , that you can acsess. 将其重命名为info不会改变任何事情,除非您还有可以访问的对象info

So the parser is exactly telling you whats wrong, is inside this line: 因此,解析器正好在告诉您出了什么问题,在此行内:

browser.tabs.get(info.tabId, function(change) {
if (!(status) || info.url.match(/https:\/\/url.com\/*/) == null) {

It tells you that info === undefined and of course you can't do something like undefined.tabId or undefined.url.match() . 它告诉您info === undefined ,当然您不能做undefined.tabIdundefined.url.match()类的事情。 because there is no such Object variables or methods inside an undefined 因为在undefined没有这样的Object变量或方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM