在 jQuery UI 中将其分配给 self

发布于 2024-09-30 19:11:05 字数 310 浏览 0 评论 0原文

我目前正在编写一个 jQuery UI 插件并调查现有的 jQuery UI 源。 在手风琴插件的 _create 方法中,this 被分配给 self (链接到源),但是 thisself 在该方法中并排使用。那么他们为什么选择分配给 self 呢?

I am currently writing a jQuery UI plugin and investigate therefore the existent jQuery UI sources.
In the _create method of the accordion plugin this is assigned to self (link to source), but then both this and self are used side by side in that method. So why do they chose to assign to self at all?

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

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

发布评论

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

评论(2

紫﹏色ふ单纯 2024-10-07 19:11:05

这都是关于上下文的。在函数本身中,selfthis 引用相同的对象,但在事件处理程序中,this 引用事件所针对的 DOM 元素。通过将 this 的值分配给 self,它们可以自由地引用这些事件处理程序中的 widget 对象或 DOM 元素。

如果仔细观察,您会发现在函数上下文中,它们仅使用 self,而在匿名函数(事件处理程序)中,它们同时使用 this>self 分别引用 DOM 元素或 Widget。

It's all about the context. In the function itself self and this refer to the same object but in the event handlers this refers to the DOM element the event is targeting. By assigning the value of this to self it leaves them free to refer to either the widget object or to the DOM element within those event handlers.

If you look closely you'll notice that within the context of the function they use self exclusively and while in the anonymous functions (event handlers) they use both this and self to refer to the DOM element or to the Widget, respectively.

活泼老夫 2024-10-07 19:11:05

我不太清楚 JQuery UI 插件是如何工作的,但我可以想象的是以下场景。我认为他们想要保留对 Widget 对象的引用而不覆盖 this (更改范围)。当函数执行时,范围不再是 Widget 对象,而是 DOM 元素。因此,他们可以使用 this 作为参考轻松添加类和所有内容。在此之前,他们使用闭包来拯救小部件 objcet 范围,并将 this 的值(现在仍然是小部件对象,当代码被解释时)分配给局部变量 self 。现在使用 self 他们可以更改对象上的字段。

I don't exactly know how JQuery UI plugins work, but what I could imagine is the following scenario. I think they wanted to keep a reference to the Widget object without overwriting this (changing the scope). When the function is executed the scope is no longer the Widget object but a DOM Element. Therefore they can easily add classes and everything using this as a reference. Before that happens they rescue the widget objcet scope using closure and assigning the value of this (now still being the widget object, when the code is interpreted) to the local variable self. Now using self they can change fields on the object.

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