使用“全球”一词是否合适?相对意义上?
请假设以下人为的 JavaScript:
function do_something() {
var x = 5;
function alert_x() {
alert(x);
}
alert_x();
}
do_something();
变量 x
是函数 do_something
的本地变量。它不是全局变量,因为它并非在每个作用域中都可用(即,在任一函数之外,例如调用 do_something
的位置)。
然而,说“变量x
对于函数alert_x
来说是全局的”是否正确?在这个意义上“全局”可以用作相对术语吗?
Please assume the following contrived JavaScript:
function do_something() {
var x = 5;
function alert_x() {
alert(x);
}
alert_x();
}
do_something();
The variable x
is local to the function do_something
. It isn't a global variable because it's not available in every scope (i.e., outside of either of the functions, such as where do_something
is called).
However, would it be proper to say that "the variable x
is global to the function alert_x
? Can "global" be used as a relative term in this sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要这样做:它违反惯例并会引起混乱。如果您将其作为标准 Javascript 对象习惯用法的一部分执行此操作,只需将
x
称为“成员变量”,或者称为“闭包变量”(因为它是由中的闭包捕获的) >alert_x
)。I recommend against doing this: it goes against convention and invites confusion. If you're doing this as part of the standard Javascript object idiom, just refer to
x
as a "member variable", or else as a "closure variable" (since it's captured by closure inalert_x
).