简体   繁体   English

js 全局和本地 scope

[英]js global and local scope

I'm experimenting a little with scopes and I stumbled upon this thing I don't understand.我正在对范围进行一些实验,但偶然发现了这个我不明白的东西。

 var GV = 'global_var' function changeVar(lv) { console.log('inside function lv is '+lv); lv='local_var' console.log('after changing value lv is '+lv); console.log('inside function GV is '+GV); } console.log('before function GV is '+GV); changeVar(GV); console.log('after function GV is '+GV);

If I run the code above the scope of lv is local, and any modification doesn't change GV in the global scope, but if I change GV to be an array and modify one of its elements in the same function, then the global GV changes too.如果我在 lv 的 scope 上方运行代码是本地的,并且任何修改都不会更改全局 scope 中的 GV,但是如果我将 GV 更改为数组并修改其在同一 ZC1C425268E68385D1AB507 中的一个元素,那么也发生变化。 Why is that?这是为什么? I have a feeling that it might be something related to instances but I don't get it.我有一种感觉,它可能与实例有关,但我不明白。

 var GV = [1,5,8] function changeVar(lv) { console.log('inside function lv is '+lv); lv[1]=[0] console.log('after changing value lv is '+lv); console.log('inside function GV is '+GV); } console.log('before function GV '+GV); changeVar(GV); console.log('after function GV is '+GV);

I thought about hoisting (so if ls is declared inside the function as a new variable) but if it was because of that shouldn't the first console.log inside the function show undefined?我考虑过提升(所以如果 ls 在 function 中声明为新变量),但如果是因为这样,那么 function 中的第一个 console.log 不应该显示未定义吗?

It is because when var GV = 'global_var' , the GV variable contains the actual string 'global_var'.这是因为当var GV = 'global_var'时,GV 变量包含实际的字符串 'global_var'。 When you pass GV to changeVar , it is passed by value, ie inside the scope of the changeVar function, a new variable lv is created and the value 'global_var' is copied to the new new variable.当您将 GV 传递给changeVar时,它是按值传递的,即在 changeVar function 的changeVar内部,会创建一个新变量lv ,并将值“global_var”复制到新的新变量中。 Immediately after the function is called, this is the state:在调用 function 之后,这是 state:

GV still contains 'global_var', and lv contains a copy of 'global_var', the exact same value. GV 仍然包含“global_var”,而 lv 包含“global_var”的副本,完全相同的值。

Now inside the local scope, lv='local_var' and lv is updated, but GV stays the same.现在在本地 scope 内部, lv='local_var'和 lv 已更新,但 GV 保持不变。 Hence, after you exit the function, GV remains unaltered, as it is residing in an altogether different memory location.因此,在您退出 function 后,GV 保持不变,因为它位于完全不同的 memory 位置。

Now let's look at the second case, where GV = [1,5,8] .现在让我们看看第二种情况,其中GV = [1,5,8] In this case, GV is assigned a JavaScript array, and remember, arrays are objects.在这种情况下,GV 被分配了一个 JavaScript 数组,记住,arrays 是对象。 Objects have references, so the GV variable contains the reference to the [1, 5, 8] array.对象有引用,因此 GV 变量包含对 [1, 5, 8] 数组的引用。 Now once you call the function changeVar , passing in GV as an argument, the reference of the array is passed and stored in lv .现在,一旦您调用 function changeVar并传入 GV 作为参数,数组的引用就会被传递并存储在lv中。 Now immediately after the function call, this is the state:现在在 function 调用之后,这是 state:

GV contains a reference to [1, 5, 8], and lv also contains the same reference to [1, 5, 8]. GV 包含对 [1, 5, 8] 的引用,而 lv 也包含对 [1, 5, 8] 的相同引用。 So it is a pass by value reference, ie the reference is passed, and the entire array is not passed as a copy to the function.所以它是传值引用,即引用传递,而不是整个数组作为副本传递给function。 Inside the function, when you execute lv[1]=[0] , it updates the original array in the memory to [1, 0, 8].在 function 内部,当你执行lv[1]=[0]时,它会将 memory 中的原始数组更新为 [1, 0, 8]。

When you exit the function, GV is still pointing to the original array, and when you print GV it shows the updated array [1, 0, 8].当您退出 function 时,GV 仍然指向原始数组,当您打印 GV 时,它会显示更新后的数组 [1, 0, 8]。

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

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