使用“全球”一词是否合适?相对意义上?

发布于 2024-09-10 15:33:24 字数 388 浏览 1 评论 0原文

请假设以下人为的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2024-09-17 15:33:24

我建议不要这样做:它违反惯例并会引起混乱。如果您将其作为标准 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 in alert_x).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文