简体   繁体   English

为什么即使声明的变量中没有“ var”关键字,我仍会收到“ Uncaught ReferenceError”?

[英]why do i get “Uncaught ReferenceError” even though variable is declared without “var” keyword?

I have come across the following code snippet: 我遇到了以下代码片段:

 (function() { bar = 5; var bar = 10; console.log("value of bar inside the self invoking function = " + bar); })(); console.log("value of bar out of function scope " + bar); 

When I execute the above code, I get: 当我执行以上代码时,我得到:

"Uncaught ReferenceError" “未捕获的ReferenceError”

for the second console 对于第二个控制台

This is due to "variable hoisting". 这是由于“可变提升”。 Variables are declared when the javascript is parsed so when it comes to execution the engine already knows all variables that will be available within a scope and can thus assign to them. 变量是在解析javascript时声明的,因此在执行时,引擎已经知道范围内将可用的所有变量,因此可以将它们分配给它们。 After the hoisting process is done your function actually looks like this. 提升过程完成后,您的功能实际上如下所示。

 (function() { var bar; bar = 5; bar = 10; console.log("value of bar inside the self invoking function = " + bar); })(); console.log("value of bar out of function scope " + bar); 

You can read more about it on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting 您可以在MDN上阅读有关它的更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/var#var_hoisting

All variables declared with var are hoisted to the top of the function in which they are declared, So if the variable is defined inside the function it will be moved to the top of the function and if it is defined in global scope, it will be moved to the top of the global context. var声明的所有变量都将提升到声明它们的函数的顶部,因此,如果在函数内部定义了该变量,则它将移到该函数的顶部,并且如果在全局范围内定义了该变量,则移至全球环境的最顶端。

(function() {
    bar = 5;
    var bar = 10;
    console.log("value of bar inside the self invoking function = " + bar);
})();

Here, in your case you have defined variable inside the anonymous function, so it will be moved to the top of this function and after hoisting it will be something like 在这里,在您的情况下,您已经在匿名函数中定义了变量,因此它将被移至该函数的顶部,并且在提升后将类似于

(function() {
    var bar;
    bar = 5;
    bar = 10;
    //...rest of the code
})();

But this variable is available only in that function's local scope , it won't be available in the global scope. 但是此变量仅在该函数的本地范围内可用,而在全局范围内不可用。 That's why you are getting Uncaught ReferenceError when you are trying to access it in global scope . 这就是为什么当您尝试在全局范围内访问它时会收到Uncaught ReferenceError的原因。

Here is the nice article explaining variable scope and variable hoisting in JavaScript. 这是一篇很好的文章,解释了JavaScript中的变量作用域和变量提升。 http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/ http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/

One thing to note is that only the variables declared using var are hoisted . 需要注意的一件事是,只有使用var声明的变量才会被提升 If you are using ES6 let keyword to declare a variable then it will not be hoisted. 如果您使用ES6 let关键字声明变量,则不会将其吊起。 So 所以

(function() {
    bar = 5;
    let bar = 10;
    console.log("value of bar inside the self invoking function = " + bar);
})();

won't work and you'll get the same error. 将无法工作,并且您将收到相同的错误。 You can read more about it here 您可以在这里了解更多信息

暂无
暂无

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

相关问题 分配给不带var关键字的变量时,为什么会出现参考错误? - Why do I get a reference error when assigning to a variable without the var keyword? 即使我已经在我的 js 文件中引用了 newTODO,为什么我的代码中仍然存在 Uncaught ReferenceError? - Why is there a Uncaught ReferenceError in my code even though I already referenced newTODO in my js file? 为什么 push 显示为未定义,即使它是一个关键字并且我没有将其声明为任何 object? - Why is push shown as undefined, even though it is a keyword and i have not declared this as any object? jQuery中的变量在未定义var时给出Uncaught ReferenceError - variable in jquery giving Uncaught ReferenceError when defined without var 为什么我会收到此错误:“未捕获的 ReferenceError:未定义 value1”? - Why do I get this error: “Uncaught ReferenceError: value1 is not defined”? 为什么会出现Uncaught ReferenceError:未定义scrollToM? - Why do I get Uncaught ReferenceError: scrollToM is not defined? 为什么会收到“未捕获的ReferenceError:未定义$(匿名函数)” - Why do I get an “Uncaught ReferenceError: $ is not defined (anonymous function)” JS:为什么允许使用和不使用`var`关键字来声明变量? - JS: Why are variables allowed to be declared both with and without the `var` keyword…? 为什么会收到未捕获的ReferenceError:未定义$ - Why do I receive an Uncaught ReferenceError: $ is not defined 为什么收到“未捕获的ReferenceError:未定义要求”? - Why I get “Uncaught ReferenceError: require is not defined”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM