简体   繁体   English

JavaScript 函数中的局部和全局变量

[英]Local and global variables inside a JavaScript function

I am learning JavaScript global and local variables, but I am confused on this particular function.我正在学习 JavaScript 全局和局部变量,但我对这个特定函数感到困惑。

var text = "top";
function print() {
    return (text);
}
print();
// Returns 'top'

I understands why it returns top.我明白为什么它返回顶部。 var text is a global variable. var text是一个全局变量。 print() function has access to it and returns text , thus returning 'top' . print()函数可以访问它并返回text ,从而返回'top'

var text = "top";
function print() {
    return (text);
    var text = "bottom";
}
print();
// Returns undefined

I have a basic knowledge of global and local variables (or so I thought).我对全局和局部变量有基本的了解(或者我认为)。 I know that the function print has access to its own local plus global variables.我知道函数print可以访问它自己的局部变量和全局变量。

I don't understand why this returns undefined .我不明白为什么这会返回undefined To my understanding, the line return text;据我了解,该行return text; retrieves global variable text , which it has access to (as shown on the first code block).检索全局变量text ,它有权访问(如第一个代码块所示)。 After returning text = 'top' , it also declares its own local variable with the same name but different value, 'bottom' .返回text = 'top' ,它还声明了自己的具有相同名称但不同值的局部变量'bottom' The local variable bottom , to my knowledge, ought to sit there because it wasn't called earlier.据我所知,局部变量bottom应该放在那里,因为它没有提前调用。

Why didn't it show top (or even shows bottom ), but instead shows undefined ?为什么它不显示top (甚至显示bottom ),而是显示undefined

JavaScript hoists your variable declaration such that your code is functionally the following: JavaScript 提升您的变量声明,以便您的代码在功能上如下:

var text = "top";
function print() {
    var text;
    return (text);
    // Unreachable code below
    text = "bottom";
}
print();
// Returns undefined

Since text , as declared in your function, is not yet defined when you hit return(text) , and text="bottom" is unreachable, print() returns undefined .由于text在您的函数中声明,在您点击return(text)时尚undefined ,并且text="bottom"无法访问,因此print()返回undefined

See What is the scope of variables in JavaScript?请参阅JavaScript 中变量的范围是什么? for more.更多。 This question relates to case 7.这个问题与案例 7 相关。

This is to do with variable hoisting这与可变提升有关

The code in your second example is executed in this order:第二个示例中的代码按以下顺序执行:

  1. Declare global variable text声明全局变量text
  2. Set value of global variable text to "top"将全局变量text值设置为“top”
  3. Declare function print声明函数print
  4. Call function print调用函数print
  5. Declare local variable text (due to hoisting)声明局部变量text (由于提升)
  6. Return value of local variable text ( undefined at this point)局部变量text返回值(此时undefined
  7. Set value of local variable text to "bottom"将局部变量text值设置为“底部”

It is executed as if it were written like this:它的执行就像是这样写的:

var text = "top";
function print() {
    var text;
    return (text);
    text = "bottom";
}
print();

As you can see, the value of text is returned before actually being defined, and therefore it is undefined .如您所见, text的值在实际定义之前返回,因此它是undefined

It's due to hoisting .这是由于吊装

Hoisting teaches that variable and function declarations are physically moved to the top of your code.提升教导变量和函数声明被物理移动到代码的顶部。

You can get samples and explanation here .您可以在此处获取示例和说明。

Your assignment,你的任务,

var text = "bottom";

comes after a return function, so it is not proper.在返回函数之后出现,所以不合适。 It is an unreachable statement.这是一个遥不可及的声明。

 var text = "top"; function print() { return (text); //var text = "bottom"; // The above assignment comes after a return function, // so it is not proper. It is unreachable statement. } alert(print()); // Returns undefined

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

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