简体   繁体   English

浏览器控制台中的变量提升问题

[英]variable hoisting in browser console problem

(1) why are the two examples different? (1) 为什么这两个例子不同?

console.log(a); 
a = 1; // this will have a is not defined;
console.log(a); 
function a(){}; // this will have no error;

(2)In browser console. (2)在浏览器控制台中。

console.log(a); 
a = 1; 
window.a === 1; // true   

why is the variable a bound to window but console.log still has an error.为什么变量a绑定到windowconsole.log仍然有错误。

window.a = 1;
console.log(a);

why is the variable a bound to window but console.log has no error.为什么变量a绑定到windowconsole.log没有错误。

the first example console.log(a); a = 1;第一个示例console.log(a); a = 1; console.log(a); a = 1; will return an error because your variable is not declared and only declared variables are hoisted using a keyword such as var for example将返回错误,因为您的变量未声明,并且仅使用诸如var之类的关键字提升已声明的变量

From MDN来自MDN

JavaScript only hoists declarations, not initializations. JavaScript 仅提升声明,而不是初始化。 If a variable is declared and initialized after using it, the value will be undefined.如果一个变量在使用后被声明和初始化,则该值将是未定义的。

The ex console.log(a); function a(){};console.log(a); function a(){}; console.log(a); function a(){}; doesn't return an error because JavaScript is adding the function declarations to memory before it executes aka Hoisting不返回错误,因为 JavaScript 正在将 function 声明添加到memory在它执行之前

in the example console.log(a); a = 1; window.a === 1;在示例console.log(a); a = 1; window.a === 1; console.log(a); a = 1; window.a === 1; will return an error because your variable is not declared, same as the first example将返回错误,因为您的变量未声明,与第一个示例相同

in this example window.a = 1; console.log(a)在这个例子中window.a = 1; console.log(a) window.a = 1; console.log(a) won't return an error because you added the variable to the global object window and gave it a value of 1 before attempting to print it's value. window.a = 1; console.log(a)不会返回错误,因为您将变量添加到全局 object window并在尝试打印它的值之前给它一个值 1。

This is called hoisting in javascript.这在 javascript 中称为hoisting

(1) (1)

console.log(a); 
a = 1; // this will have a is not defined;

In the above snippet, a is assigned after console , so it is throwing an error.在上面的代码片段中, a 是在console之后分配的,所以它会抛出一个错误。

In case if you have如果你有

 console.log(a);
 var a = 1;

The result will be undefined printed in the console.结果将undefined打印在控制台中。 It is because of hoisting.是因为吊装。 Javascript will execute the above in the following order. Javascript 将按以下顺序执行上述操作。

var a;
console.log(a);
a = 1;

Hoisting is executing the initialisation first and rest of the execution will go as per the flow.吊装首先执行初始化,执行的 rest 将按照流程执行 go。

var a; is executed first and a is initialized to the scope.首先执行并将a初始化为 scope。 Hence console.log(a) will not throw any error.因此console.log(a)不会抛出任何错误。 At the time of its execution, a value is undefined .在其执行时, a值是undefined

(2) (2)

console.log(a); 
a = 1; 
window.a === 1; // true   

Whenever you try to do anything in the console, all the variables and functions will be registered with the window scope.每当您尝试在控制台中执行任何操作时,所有变量和函数都将注册到window scope。 So, when you do a = 1 in console, it will be window.a .因此,当您在控制台中执行a = 1时,它将是window.a

(3) (3)

console.log(a); 
function a(){}; // this will have no error;

The same hoisting applies to functions also.同样的hoisting也适用于功能。 When you created a function using constructor like above, it will be hoisted .当您使用上述构造函数创建 function 时,它将被hoisted If you execute the below snippet you will get error because it is not hoisted .如果您执行以下代码段,您将收到错误,因为它没有被hoisted

console.log(a)
a = function(){}

why is the variable a bound to window but console.log still has an error.为什么变量 a 绑定到 window 但 console.log 仍然有错误。

The variable is registered with window scope, but it is not hoisted.变量注册到window scope,但没有被提升。 So you will get error.所以你会得到错误。 Hoisting will only work when you initialize a variable. Hoisting仅在初始化变量时起作用。

To get further details about hoisting , feel free to read docs要获取有关hoisting的更多详细信息,请随时阅读文档

console.log(a); a = 1; // this will have a is not defined; console.log(a); function a(){}; // this will have no error;

The function keyword is bound before any code will be excuted in JS. function关键字在 JS 中执行任何代码之前被绑定。

 console.log(a); a = 1; window.a === 1; // true

why is the variable a bound to window but console.log still has an error.为什么变量 a 绑定到 window 但 console.log 仍然有错误。

 window.a = 1; console.log(a);

why is the variable a bound to window but console.log has no error.为什么变量 a 绑定到 window 但 console.log 没有错误。

You're already in the window context.您已经在 window 上下文中。 So a is the same as window.a .所以awindow.a相同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM