简体   繁体   English

jQuery和IIFE包装器的说明?

[英]Jquery and IIFE wrapper clarification?

The window can not be redefined like we see in this example: 无法像在此示例中看到的那样重新定义window

http://i.stack.imgur.com/IYX0p.jpg http://i.stack.imgur.com/IYX0p.jpg 在此处输入图片说明

So why jquery starts itself with window ( and it even uses the same name)? 那么,为什么jquery以window开头(甚至使用相同的名称)?

(function( window, undefined ) {...})( window );

I mean , it could have use window inside it... 我的意思是,它可能在其中使用window ...

ps the undefined trick has nothing to do with it... PS undefined把戏与它无关...

I guess a possible explanation is to make JQuery easier to port in other contexts, where the global object may not be named window , or even embedded in some other scripts, in the case window refers to a valid token in the current scope (or in the activation object). 我猜可能有一种解释是,它可以使JQuery在其他上下文中更容易移植,在这种情况下,全局对象可能不会被命名为window ,甚至可能不会嵌入到其他一些脚本中,以防window引用当前作用域中的有效令牌(或激活对象)。

Anyway, the only reason why I think this may be useful is because the jQuery code can use the window name without worry, because any vendor can bind the name to whatever object it wants, only by changing a single line of the jQuery source (the last one). 无论如何,我认为这可能有用的唯一原因是因为jQuery代码可以使用window名称而不必担心,因为任何供应商都可以仅通过更改jQuery源代码的一行来将名称绑定到所需的任何对象(最后一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM