在 javascript 中,这个语法 `(function(window,undfined)){}(window)` 完成了什么

发布于 2024-10-01 13:40:00 字数 506 浏览 3 评论 0原文

我试图了解如何像 jQuery 一样将 JavaScript 事件“链接”在一起。我在这里发现了一个与我的目标相似的问题,但我不明白答案中的代码。

代码源

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

这是什么意思?它在做什么?这让我想起了 Jquery 的 $(document).ready(){} 函数,但我不知道为什么这个人把他的代码包装在这个传递 window 的匿名函数中和未定义

我的最终目标是弄清楚如何通过像 jQuery 一样将方法链接在一起来在对象上执行方法。我知道 jQuery 已经做到了这一点,但我主要是为了作为一名开发人员的成长而研究这一点。

I am trying to understand how to "chain" JavaScript events together like jQuery does. I found a question here on S.O. that was similar to my goal, but I do not understand the code in the answer.

Code Source

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

What does that mean? What is it doing? It reminds me of Jquery's $(document).ready(){} function, but I don't know why this person wrapped his code in this anonymous function that passes window and undefined.

My ultimate goal is to figure out how to execute methods on an object by chaining methods together like jQuery. I know that jQuery already does this but I am looking into this primarily for growth as a developer.

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

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

发布评论

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

评论(2

白云悠悠 2024-10-08 13:40:00

它定义了一个函数(使用 函数运算符,而不是函数语句)。它周围的括号确保它被视为运算符而不是语句。

然后它立即执行它,并将 window 作为参数传递。

本质上,这与:

var myFunction = function( window, undefined ) { 
...etc...
};
myFunction(window);

...但没有间隙变量。

这与 jQuery 风格的函数链接无关,其中每个方法实际上都以 return this 结束(因此在另一个方法的返回值上调用一个方法与在原始对象上调用它相同)。

It defines a function (using a function operator as opposed to a function statement). The parenthesis around it ensure that it is treated as the operator rather than the statement.

It then executes it immediately, passing window as an argument.

Essentially, this is the same as:

var myFunction = function( window, undefined ) { 
...etc...
};
myFunction(window);

… but without the interstitial variable.

This has nothing to do with jQuery style function chaining, where each method effectively ends with return this (so calling a method on the return value of another method is the same as calling it on the original object).

一桥轻雨一伞开 2024-10-08 13:40:00

当调用函数时使用的参数少于其签名所包含的参数时,尾部参数将被分配值undefined

因此,上面是一种获取 undefined 值的迂回方法,即使某些疯子通过说 var undefined= 'hello'; 重新定义了它。 (无论如何,这在 ECMAScript 第五版的“严格模式”中是非法的,但 JavaScript 编码人员有时会做一些奇怪的事情。)

尽管如此,传入 window 并没有真正好的理由......如果不能依赖 window,获取 window 的传统方法是直接调用函数并使用 this

不管怎样,这只是针对病态作者 JavaScript 的防御性编码。在编写自己的代码时,这不是您应该担心的事情(在任何情况下,您都无法阻止某人可能弄乱其 JS 环境的每一种方式),并且这与链接无关。

When a function is called with fewer arguments than its signature contains, the trailing arguments are assigned the value undefined.

So the above is a roundabout way of getting hold of the undefined value even if some lunatic has redefined it by saying var undefined= 'hello';. (This is illegal anyway in ECMAScript Fifth Edition's ‘strict mode’, but JavaScript coders do some weird things sometimes.)

There isn't really a good reason for passing in window like this though... the traditional way to get window if you can't rely on window is to call a function directly and use this.

Either way, this is simply defensive coding against pathological author JavaScript. It's not something you should worry about whilst writing your own code (in any case there's no way you can stop every way someone might mess up their JS environment), and it's nothing to do with chaining.

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