为什么 jQuery 有一个“window=this” 一开始就说这会加快对窗口的引用?

发布于 2024-07-14 01:10:18 字数 170 浏览 9 评论 0原文

当我打开 jQuery 的源代码时,我发现了这一行。

var 
// Will speed up references to window, and allows munging its name.
window = this

这条线路为何以及如何加速?

When I open jQuery's source code I find this line.

var 
// Will speed up references to window, and allows munging its name.
window = this

Why and how this line will speed up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

银河中√捞星星 2024-07-21 01:10:19
  1. javascript 函数具有词法作用域
  2. jQuery 包装了整个函数在 匿名函数 中实现
  3. 当所述函数开始执行时,它正在执行“全局”范围(即 this == window)。

“window = this;” 只是在该范围内创建一个本地标识符,以便对其的引用不必在本地范围之外“冒泡”来解析。

  1. javascript functions have lexical scope
  2. jQuery wraps its entire implementation in an anonymous function
  3. when said function begins execution it is executing in the "global" scope (ie this == window).

"window = this;" simply creates a local identifier in that scope so that references to it do not have to "bubble up" outside of the local scope to resolve.

◇流星雨 2024-07-21 01:10:19

与必须解析为 window 对象的 window 相比,this 对于 JavaScript 来说引用起来会更快。

this would be faster for javascript to reference to, as compared to window which would have to be resolved to the window object.

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