如何使函数内定义的变量 newUser 成为全局变量?
我使用 chrome.extension.getBackgroundPage()
将变量 newUser
从 options.html
发送到 background.html
this
document.getElementById("save").addEventListener(
"click", function ()
{
var newUser = document.getElementById("getEmail").value;
var bkg = chrome.extension.getBackgroundPage();
bkg.saveNewUser(newUser);
console.log("newUser " + newUser);
} , false)
在 background.html
我有
function saveNewUser (newUser)
{
newUser = newUser; //this should be global?
console.log("newUser from options page " + newUser);
}
console.log("newUser from options page " + newUser);
,但 newUser
是该函数的本地函数。我的理解是,如果变量保存在没有关键字 var
的函数内,它应该是全局的。但在本例中却并非如此。函数外部的 console.log
抛出 Uncaught ReferenceError: newUser is not Defined
错误。
我做错了什么以及如何修复在函数外部使用 newUser
的范围?
谢谢。
编辑
我在background.html
中尝试了以下操作,但仍然收到 newUser 未定义错误:
var extension_user
function saveNewUser (newUser)
{
extension_user = newUser; //this should be global
console.log("extension_user from options page " + extension_user);
}
console.log("extension_user from options page " + extension_user);
更新
我更改了两个页面以反映答案和评论,但是仍在函数之外,变量未定义。我做错了什么?
options.html
document.getElementById("save").addEventListener(
"click", function ()
{
var newUserEmail = document.getElementById("getEmail").value;
var bkg = chrome.extension.getBackgroundPage();
bkg.saveNewUser(newUserEmail);
console.log("newUserEmail " + newUserEmail);
} , false)
background.html
var newUser
function saveNewUser (newUserEmail)
{
window.newUser = newUserEmail; //this should be global
console.log("newUser from options page inside function" + newUser);
}
console.log("newUser from options page " + newUser);
I send the variable newUser
from options.html
to background.html
with chrome.extension.getBackgroundPage()
like this
document.getElementById("save").addEventListener(
"click", function ()
{
var newUser = document.getElementById("getEmail").value;
var bkg = chrome.extension.getBackgroundPage();
bkg.saveNewUser(newUser);
console.log("newUser " + newUser);
} , false)
In background.html
I have
function saveNewUser (newUser)
{
newUser = newUser; //this should be global?
console.log("newUser from options page " + newUser);
}
console.log("newUser from options page " + newUser);
but newUser
is local to the function. My understanding is that if a variable is saved inside a function without the keyword var
it should be global. But in this case it is not. The console.log
outside the function throws Uncaught ReferenceError: newUser is not defined
error.
What am I doing wrong and how can I fix the scope to use newUser
outside the function?
Thanks.
Edit
I tried the following in background.html
but I still get newUser undefined error:
var extension_user
function saveNewUser (newUser)
{
extension_user = newUser; //this should be global
console.log("extension_user from options page " + extension_user);
}
console.log("extension_user from options page " + extension_user);
Update
I changed both pages to reflect the answers and comments but still outside the function the variable is undefined. What am I doing wrong?
options.html
document.getElementById("save").addEventListener(
"click", function ()
{
var newUserEmail = document.getElementById("getEmail").value;
var bkg = chrome.extension.getBackgroundPage();
bkg.saveNewUser(newUserEmail);
console.log("newUserEmail " + newUserEmail);
} , false)
background.html
var newUser
function saveNewUser (newUserEmail)
{
window.newUser = newUserEmail; //this should be global
console.log("newUser from options page inside function" + newUser);
}
console.log("newUser from options page " + newUser);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想将其保存为全局变量,你可以在函数中更改变量的名称:
或者可以使用窗口作用域(与全局作用域相同):
所有全局变量本质上都是函数的属性。窗口对象。
If you want to save it as a global variable, you can either change the name of the variable in the function:
or you can use the window scope (which is the same as the global scope):
All global variables are essentially properties of the window object.
在调用函数
saveNewUser
之前,newUser
变量并不存在于全局范围内。因此它给出了参考错误。只需将全局范围内的变量newUser
声明为var newUser;
即可。那应该可以解决问题。声明变量时始终使用var
关键字。这是一个很好的编程实践。切勿在局部作用域内声明全局变量。你已经知道为什么了。编辑:
这是澄清我的答案的代码:
The
newUser
variable does not exist in the global scope until the functionsaveNewUser
is called. Thus it gives a reference error. Just declare the variablenewUser
in the global scope asvar newUser;
. That should solve the problem. Always use thevar
keyword when declaring variables. It's a good programming practice. Never declare global variables within local scopes. You already know why.Edits:
Here's the code to clarify my answer: