[英]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.