[英]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:第二个示例中的代码按以下顺序执行:
text
text
text
to "top"text
值设置为“top”print
print
print
print
text
(due to hoisting)text
(由于提升)text
( undefined
at this point)text
返回值(此时undefined
)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
。
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.