简体   繁体   English

JavaScript中的全局与本地示例

[英]Global vs. Local in JavaScript example

I'm trying to learn the basics of JavaScript and ran into something I'm not totally 100% about. 我正在尝试学习JavaScript的基础知识,并且碰到了一些我并非完全100%了解的内容。 Here is the code without declaring the my_number variable inside the function: 以下是未在函数内部声明my_number变量的代码:

 var my_number = 7; // this has global scope var timesTwo = function(number) { my_number = number * 2; console.log("Inside the function my_number is: "); console.log(my_number); }; timesTwo(6); console.log("Outside the function my_number is: ") console.log(my_number); 

This outputs to: Inside the function my_number is: 12 Outside the function my_number is: 12 输出到:函数my_number内为:12函数my_number内为:12

First, from what I've read if you don't declare your variable inside a function it will take on the value of a global variable of the same name. 首先,根据我的读物,如果不在函数中声明变量,它将采用同名的全局变量的值。 My guess as to what's happening here is that when the function call passes the number 6 it passed it to the function, does the multiplication and logs the result (12) to the console. 我对这里发生的事情的猜测是,当函数调用传递数字6它将其传递给函数,进行乘法运算并将结果(12)记录到控制台。 Once the function has ended it keeps that value 12 (essentially overwrites the global value?) and logs the number 12 again to the console. 函数结束后,它将保留该值12 (实际上是覆盖全局值吗?),然后将数字12再次记录到控制台。

One the other hand, when I declare the variable inside the function, var my_number = number * 2; 另一方面,当我在函数内部声明变量时,var my_number = number * 2; it work just like I expect it to and prints out 12 Inside the function and uses the global variable 7 for the last line of code. 它的工作方式与我期望的一样,并在函数内部打印出12 ,并在代码的最后一行使用全局变量7

  var my_number = 7; // this has global scope var timesTwo = function(number) { var my_number = number * 2; console.log("Inside the function my_number is: "); console.log(my_number); }; timesTwo(6); console.log("Outside the function my_number is: ") console.log(my_number); 

Inside the function my_number is: 12 函数my_number内是:12

Outside the function my_number is: 7 函数my_number之外是:7

Am I understanding this correctly or not? 我是否正确理解了这一点? Especially the first snippet of code. 尤其是第一段代码。 I'd like to fully grasp this concept before moving on, it seems important to me. 我想在继续之前完全掌握这个概念,对我来说似乎很重要。

Your understanding seems to be fine, though I'm not sure of the following statement: 您的理解似乎还不错,尽管我不确定以下说法:

Once the function has ended it keeps that value 12 (essentially overwrites the global value?) 函数结束后,它将保持该值12 (实际上是覆盖全局值吗?)

This does have nothing to do with the function ending. 这与函数结尾无关。 As there was no local variable with that name in the function, every occurence of my_number referred directly to the global variable, which was written to in the assignment and read from in the logging statement. 由于函数中没有使用该名称的局部变量,因此my_number每次出现都直接引用全局变量,该全局变量在赋值中写入并在日志记录语句中读取。 The timesTwo functin doesn't have its own copy of the variable. timesTwo没有自己的变量副本。

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

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