简体   繁体   English

Javascript吊装

[英]Javascript hoisting

I am learning javascript hoisting feature, and find the following codes are really confusing: 我正在学习javascript提升功能,发现以下代码确实令人困惑:

var a = 1;
function b() {
    a = 10;
    return;
    function a() {}
}
b();
alert(a);

The output is 1 . 输出为1 As far as I know, because of hoisting , the codes above are equivalent to 据我所知,由于hoisting ,以上代码等同于

var a;
function b() {      
    function a() {}
    a=10;
    return;
}
a=1;
b();
alert(a);

What happens when a function and a variable has the same name a ? 当发生什么functionvariable具有相同的名称a

Within b , a local variable a is first set to a function, and then the value 10 . b ,首先将局部变量a设置为一个函数,然后将其设置为10 The outer variable a remains unaffected, since it is shadowed by the local variable a within b . 外部变量a保持不受影响,因为它被b的局部变量a b Perhaps this roughly equivalent code will help illustrate: 也许这段大致等效的代码将有助于说明:

var a = 1;
function b() {
    var a;
    a = function a() { };
    a = 10;
    return;
}
b(); // Basically a no-op
alert(a);

What happens when a function and a variable has the same name a? 函数和变量的名称相同时会发生什么?

Nothing special. 没什么特别的。 One assignment to a overwrites the other because they are both values. 其中分配给a覆盖其他,因为它们都值。

console.log(a) // a points to a function here
var a = 4
console.log(a) // a points to 4 here
function a() {}
console.log(a) // a also points to 4 here!

By the way, a variable in a scope outside of a function can only be modified by that function if the variable isn't local to the function. 顺便说一句,如果该函数不在函数的作用域内,则该变量只能在该函数不是本地变量的情况下才能被该函数修改。

var a = 4
;(function() {
    a = 5
})() // <-- Immediately calling the function here
console.log(a) // a is now 5
;(function() {
    a = 6
    var a
})()
// a is still 5 because in the previous function,
// a was local to the function's scope
console.log(a)

Function parameters are implicitly local to the variable, so they automatically "shadow" globals that share the same name. 函数参数隐式地位于变量的局部,因此它们会自动“共享”具有相同名称的全局阴影。

The function: 功能:

function b() {
    a = 10;
    return;
    function a() {}
}

Is basically the same thing as this, due to hoisting pushing the function to the top of its scope: 由于hoisting将功能推到其作用域的顶部,因此基本上与此相同:

function b() {      
    function a() {}
    a = 10;
    return;
}

It is also important to note that function a() {} is the same as writing var a = function() {} . 同样重要的是要注意, function a() {}与编写var a = function() {} So now, we have something like this: 所以现在,我们有了这样的东西:

function b() {      
    var a = function() {}
    a = 10;
    return;
}

Since inside of the function b() a variable named a was declared, b() is only working with and changing that a within its local scope, so the a that is declared outside of the function b() is left untouched. 由于函数内b()命名的变量a是宣称, b()只与工作并改变 a本地范围内,所以a是功能之外声明b()是保持不变。

If we omit function a() {} , then the value of the global a will be assigned from inside the function b() 如果我们省略函数a(){} ,那么全局a的值将从函数b()中分配

var a = 1;
function b() {
  a = 10; 
}
b();
console.log(a); // 10

Using function a() {} inside the function b() will have the same effect as if we declared var a=10; 函数b()中使用函数a(){}将具有与声明var a = 10相同的效果 This will limit the scope of a to inside the function b() and won't bleed outside its scope. 这会将a的范围限制为函数b()的内部,并且不会在其范围之外流血。

var a = 1;
function b() {
  var a = 10; 
}
b();
console.log(a); // 1

Here is an easy read article on Hoisting: https://medium.com/@bouguerra_70679/hoisting-464979b60282 这是有关吊装的易读文章: https : //medium.com/@bouguerra_70679/hoisting-464979b60282

What happens when a function and a variable has the same name a? 函数和变量的名称相同时会发生什么?

In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. 在JavaScript中, 函数是一流的对象,因为它们可以像其他任何对象一样具有属性和方法。

(What distinguishes them from other objects is that functions can be called.) (它们与其他对象的区别在于可以调用函数。)

In other words, functions are defined as variables of type function, and can be passed to other functions, stored in arrays, etc. 换句话说,函数被定义为函数类型的变量,并且可以传递给其他函数,存储在数组中等。

So when a function and a variable has the same name a, there could be some conflict, as in your example: 因此,当函数和变量具有相同的名称a时,可能会发生一些冲突,如您的示例所示:

var a;
function b() {      
    function a() {} // variable a of type function shadows variable a in the outer scope
    a=10; // variable a defined in the inner scope is now of number, instead of function any more
    return;
}
a=1;
b();
alert(a); // 1

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

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