javascript 参考错误:未定义变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17583544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:52:55  来源:igfitidea点击:

ReferenceError: variable is not defined

javascriptvariablesdefinitionvar

提问by Andrew Liu

I met this issue sometimes but still don't know what causes it.

我有时会遇到这个问题,但仍然不知道是什么原因造成的。

I have this script in the page:

我在页面中有这个脚本:

$(function(){
    var value = "10";
});

But the browser says "ReferenceError: value is not defined". However if I go to the browser console and input either

但浏览器显示“ReferenceError: value is not defined”。但是,如果我转到浏览器控制台并输入

10

or

或者

var value = "10";

either of them can return 10. What is the problem with my script?

它们中的任何一个都可以返回 10。我的脚本有什么问题?

回答by McGarnagle

It's declared inside a closure, which means it can only be accessed there. If you want a variable accessible globally, you can remove the var:

它在闭包内声明,这意味着它只能在那里访问。如果你想要一个全局可访问的变量,你可以删除var

$(function(){
    value = "10";
});
value; // "10"

This is equivalent to writing window.value = "10";.

这相当于写window.value = "10";.

回答by Micha? Per?akowski

Variables are available only in the scope you defined them. If you define a variable inside a function, you won't be able to access it outside of it.

变量仅在您定义它们的范围内可用。如果在函数内部定义变量,则无法在函数外部访问它。

Define variable with varoutside the function (and of course before it) and then assign 10to it inside function:

var在函数外部(当然在它之前)定义变量,然后10在函数内部分配给它:

var value;
$(function() {
  value = "10";
});
console.log(value); // 10

Note that you shouldn't omit the first line in this code (var value;), because otherwise you are assigning value to undefined variable. This is bad coding practice and will not work in strict mode. Defining a variable (var variable;) and assigning value to a variable (variable = value;) are two different things. You can't assign value to variable that you haven't defined.

请注意,您不应省略此代码中的第一行 ( var value;),否则您将给未定义的变量赋值。这是不好的编码习惯,在严格模式下不起作用。定义变量 ( var variable;) 和为变量赋值 ( variable = value;) 是两件事。您不能为尚未定义的变量赋值。

It might be irrelevant here, but $(function() {})is a shortcut for $(document).ready(function() {}), which executes a function as soon as document is loaded. If you want to execute something immediately, you don't need it, otherwise beware that if you run it before DOM has loaded, value will be undefineduntil it has loaded, so console.log(value);placed right after $(function() {})will return undefined. In other words, it would execute in following order:

它在这里可能无关紧要,但它$(function() {})是 的快捷方式$(document).ready(function() {}),它在加载文档后立即执行函数。如果您想立即执行某些内容,则不需要它,否则请注意,如果您在 DOM 加载之前运行它,则 value 将为undefined直到它加载console.log(value);完毕,因此放在之后$(function() {})将返回undefined。换句话说,它将按以下顺序执行:

var value;
console.log(value);
value = "10";

See also:

也可以看看:

回答by Bob

Got the error (in the function init) with the following code ;

使用以下代码得到错误(在函数 init 中);

"use strict" ;

var hdr ;

function init(){ // called on load
    hdr = document.getElementById("hdr");
}

... while using the stock browser on a Samsung galaxy Fame ( crap phone which makes it a good tester ) - userAgent ; Mozilla/5.0 (Linux; U; Android 4.1.2; en-gb; GT-S6810P Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

...在三星galaxy Fame上使用股票浏览器(垃圾手机使其成为一个很好的测试者) - userAgent; Mozilla/5.0 (Linux; U; Android 4.1.2; en-gb; GT-S6810P Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

The same code works everywhere else I tried including the stock browser on an older HTC phone - userAgent ; Mozilla/5.0 (Linux; U; Android 2.3.5; en-gb; HTC_WildfireS_A510e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

相同的代码适用于我尝试过的其他任何地方,包括旧 HTC 手机上的股票浏览器 - userAgent ;Mozilla/5.0 (Linux; U; Android 2.3.5; en-gb; HTC_WildfireS_A510e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

The fix for this was to change

解决这个问题的方法是改变

var hdr ;

to

var hdr = null ;