将窗口和未定义传递给立即调用的匿名函数。为什么? [英] Passing window and undefined to an immediately invoked anonymous function. Why?

查看:175
本文介绍了将窗口和未定义传递给立即调用的匿名函数。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么我们需要将窗口和未定义传递到这个jquery插件?

我见过jQuery源代码这样做:

I've seen jQuery source code does this:


(function(window, undefined){

...

}(window))

我明白为什么包含undefined是有用的,如果有人在哪里改变undefined之前。但窗户不能改变。据我所知,它甚至不需要使用,对吧?这怎么可能有用呢?

I get why is it useful to include undefined, if someone where to change "undefined" before. But window cant be changed. For all I know, it doesnt even need to be used, right? How could this be useful?

推荐答案

微优化。

拥有 window 作为局部变量比全局变量略快。

Having window as a local variable is marginally faster than a global variable.

它也会更好地缩小。我们现在可以将函数参数缩小为 w 并使用 w.setTimeout 等而不是窗口.setTimeout

It also minifies better. We can now minify the function parameter to w and use w.setTimeout etc instead of window.setTimeout.

更少字节=更好

这篇关于将窗口和未定义传递给立即调用的匿名函数。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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