Javascript函数内的局部和全局变量

时间:2021-02-05 16:46:39

I am learning JS global and local variables, but I am confused on this particular function.

我正在学习JS全局和局部变量,但我对这个特定函数感到困惑。

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

I understands why it returns top. var text is a global variable. print() function has access to it and returns text, thus returning 'top'.

我明白为什么它会回归顶峰。 var text是一个全局变量。 print()函数可以访问它并返回文本,从而返回'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. To my understanding, the line return text; retrieves global variable text, which it has access to (as shown on the first code block). After returning text = 'top', it also declares its own local variable with the same name but different value, 'bottom'. The local variable bottom, to my knowledge, ought to sit there because it wasn't called earlier.

我不明白为什么这会返回undefined。据我所知,行返回文字;检索它有权访问的全局变量文本(如第一个代码块所示)。在返回text ='top'之后,它还声明了自己的局部变量,其名称相同但值不同,为'bottom'。据我所知,局部变量底部应该坐在那里,因为之前没有调用它。

Why didn't it show top (or even shows bottom) but instead shows undefined?

为什么它没有显示顶部(甚至显示底部),而是显示未定义?

4 个解决方案

#1


6  

Javascript hoists the 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

由于在命中return(text)时尚未定义函数中声明的文本,并且text =“bottom”无法访问,因此print()将返回undefined

See What is the scope of variables in Javascript for more. This question relates to case 7.

有关更多信息,请参阅Javascript中的变量范围。这个问题涉及案例7。

#2


2  

This is to do with variable hoisting

这与可变吊装有关

The code in your second example is executed in this order:

第二个示例中的代码按以下顺序执行:

  1. Declare global variable text
  2. 声明全局变量文本

  3. Set value of global variable text to "top"
  4. 将全局变量文本的值设置为“top”

  5. Declare function print
  6. 声明功能打印

  7. Call function print
  8. 呼叫功能打印

  9. Declare local variable text (due to hoisting)
  10. 声明局部变量文本(由于提升)

  11. Return value of local variable text (undefined at this point)
  12. 局部变量文本的返回值(此时未定义)

  13. Set value of local variable text to "bottom"
  14. 将局部变量文本的值设置为“bottom”

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的值在实际定义之前返回,因此未定义。

#3


0  

It's due to hosting.

这是由于托管。

hoisting teaches that variable and function declarations are physically moved to the top your coding

hoisting教导变量和函数声明在物理上移动到编码的顶部

You can get samples and explanation here

你可以在这里获得样品和解释

#4


-1  

Your assignment:

var text = "bottom";

comes after a return function so it is not proper, it is 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 statemen
}
alert(print());
// returns undefined

#1


6  

Javascript hoists the 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

由于在命中return(text)时尚未定义函数中声明的文本,并且text =“bottom”无法访问,因此print()将返回undefined

See What is the scope of variables in Javascript for more. This question relates to case 7.

有关更多信息,请参阅Javascript中的变量范围。这个问题涉及案例7。

#2


2  

This is to do with variable hoisting

这与可变吊装有关

The code in your second example is executed in this order:

第二个示例中的代码按以下顺序执行:

  1. Declare global variable text
  2. 声明全局变量文本

  3. Set value of global variable text to "top"
  4. 将全局变量文本的值设置为“top”

  5. Declare function print
  6. 声明功能打印

  7. Call function print
  8. 呼叫功能打印

  9. Declare local variable text (due to hoisting)
  10. 声明局部变量文本(由于提升)

  11. Return value of local variable text (undefined at this point)
  12. 局部变量文本的返回值(此时未定义)

  13. Set value of local variable text to "bottom"
  14. 将局部变量文本的值设置为“bottom”

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的值在实际定义之前返回,因此未定义。

#3


0  

It's due to hosting.

这是由于托管。

hoisting teaches that variable and function declarations are physically moved to the top your coding

hoisting教导变量和函数声明在物理上移动到编码的顶部

You can get samples and explanation here

你可以在这里获得样品和解释

#4


-1  

Your assignment:

var text = "bottom";

comes after a return function so it is not proper, it is 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 statemen
}
alert(print());
// returns undefined