为什么jQuery将window对象传递到它们的作用域中 [英] Why does jQuery pass the window object into their scope

查看:142
本文介绍了为什么jQuery将window对象传递到它们的作用域中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我自己写了一个项目的js库,我有一个问题。像大多数其他库一样,为了保留我的变量范围,我将代码包装在此:

Ok so I'm writing myself a js library for a project and I have a question. Like most other libraries out there, to preserve my variable scope I am wrapping my code in this:

(function() {
// my code here
})();

现在我的问题是:我注意到jQuery传递了window对象并设置了自己的文档对象这个:

Now my question is this: I notice jQuery passes in the window object and sets its own document object like this:

(function(window) {
var document = window.document;
})(window);

有谁知道他们为什么这样做?

Does anyone know why they do this?

推荐答案

是的!由于此函数中的窗口是一个局部变量,现在它允许缩小其名称。
同时访问局部变量应该比全局变量更快。

Yes! Since the window in this function is a local variable now it allows minify its name. Also access to the local variables should be faster than to the global ones.

这篇关于为什么jQuery将window对象传递到它们的作用域中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆