简体   繁体   English

为什么此全局Javascript变量在函数内部和外部的行为有所不同?

[英]Why does this global Javascript variable behave differently inside and outside of a function?

I'm trying to understand why my global variable 'imageUrl' behaves differently inside and outside of the function 'genericOnClick()' 我试图了解为什么我的全局变量'imageUrl'在函数'genericOnClick()'内外的行为不同

var imageUrl

var id = chrome.contextMenus.create({
    "title": "Add to JC Queue",
    "contexts": ["image"],
    "onclick": genericOnClick
});

function genericOnClick(info) {
    imageUrl = info.srcUrl;
    console.log(imageUrl);
    chrome.tabs.create({
        url: chrome.extension.getURL('dialog.html'),
        active: false
    }, function (tab) {
        // After the tab has been created, open a window to inject the tab
        chrome.windows.create({
            tabId: tab.id,
            type: 'popup',
            focused: true
        });
    });
}

console.log(imageUrl);

Please let me know where I am going wrong: 请让我知道我要去哪里了:

  1. Declare imageUrl as a global variable 将imageUrl声明为全局变量
  2. Declare id as a global variable and run a function OnClick() 将id声明为全局变量并运行函数OnClick()
  3. Log imageUrl to the console inside the function (it displays fine) 将imageUrl记录到函数内部的控制台中(显示正常)
  4. Log imageUrl to the console after the function runs (it is undefined) 函数运行后将imageUrl登录到控制台(未定义)

When the second console.log is run, the function has not been called, therefore the variable hasn't been assigned anything yet. 当运行第二个console.log ,尚未调用该函数,因此尚未为变量分配任何东西。 However, inside the function it has received a value, and that's what you see. 但是,在函数内部它已经收到一个值,这就是您所看到的。

好吧,当您调用函数之前记录日志时, imageURL undefined ,代码末尾的日志在调用函数之前运行。

The last line ( console.log(imageUrl); ) runs almost immediately after you declare imageUrl without a value, so it is undefined at that point. 最后一行( console.log(imageUrl); )在声明不带任何值的imageUrl之后几乎立即运行,因此在这一点上它是未定义的。

Try setting imageUrl to an initial value, and you'll see that that value will get logged. 尝试将imageUrl设置为初始值,您会看到该值将被记录。

暂无
暂无

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

相关问题 Javascript:为什么数组变量赋值在此函数内部和外部的行为有所不同? - Javascript: Why array variable assignment is behaving differently inside and outside this function? 为什么jQuery的行为与javascript不同? - Why does jQuery behave differently than javascript? 为什么/ * * /注释的行为不同? Javascript错误? - Why does /* */ comment behave differently ? Javascript bug? 为什么 oninput 事件在 Angular 中的行为与在 JavaScript 中的行为不同? - Why does the oninput event behave differently in Angular than it does in JavaScript? 为什么这个函数表达式的行为与函数声明不同? - Why does this function expression behave differently than a function declaration? 为什么无服务器中的本地事件变量表现不同? - Why does the local event variable in serverless behave differently? 为什么这个javascript对象在有和没有模块模式的情况下表现不同? - Why does this javascript object behave differently with and without a module pattern? 为什么&#39;&lt;&lt;&#39;运算符在Javascript和PHP中的行为有时会有所不同? - Why does the '<<' operator sometimes behave differently in Javascript and PHP? 为什么JavaScript“delete”运算符在不同的浏览器中表现不同? - Why does the JavaScript “delete” operator behave differently in different browsers? 为什么重新定义自己的功能在Chrome / IE和Firefox中表现不同? - Why does a function redefining itself behave differently in Chrome/IE and Firefox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM