JavaScript 内部的事件处理是如何工作的?
具体来说是Spidermonkey。
我知道您编写函数并将它们附加到事件来处理它们。
onClick
处理程序在哪里定义?JS 引擎如何知道在用户单击时触发 onClick
事件?
任何关键字、设计模式、链接等都值得赞赏。
更新
我发现聚合链接在这里很有用:
http://www.w3.org/TR/DOM-Level-2-Events/events.html
https://github.com/joyent/node/blob/master/src/node_events.cc
http://mxr.mozilla.org/mozilla/source/dom/src/events/nsJSEventListener.cpp
Specifically Spidermonkey.
I know you write functions and attach them to events to handle them.
Where is the onClick
handler defined and how does the JS engine know to fire onClick
events when the user clicks?
Any keywords, design patterns, links, etc are appreciated.
UPDATE
Aggregating links I find useful here:
http://www.w3.org/TR/DOM-Level-2-Events/events.html
https://github.com/joyent/node/blob/master/src/node_events.cc
http://mxr.mozilla.org/mozilla/source/dom/src/events/nsJSEventListener.cpp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SpiderMonkey 本身没有任何涉及事件处理的东西。事件纯粹是 DOM 的事情。
点击事件是由浏览器代码(嵌入 SpiderMonkey 的东西)触发的,而不是由 SpiderMonkey 本身触发的。请参阅 http://hg.mozilla.org/ mozilla-central/file/e60b8be7a97b/content/events/src/nsEventStateManager.cpp 负责的代码调度诸如点击之类的东西。
浏览器还定义了 setter 方法,这些方法接受对 onclick 属性的赋值并将其转换为事件侦听器注册。请参阅http://hg.mozilla.org/ mozilla-central/file/e60b8be7a97b/dom/base/nsDOMClassInfo.cpp#l7624 调用自
nsEventReceiverSH::SetProperty
并处理名称(在此代码中为id
)通过IsEventName
测试的属性。当注册事件侦听器并触发事件时,事件调度程序将管理对侦听器的调用;您找到的 nsJSEventListener 链接是将 C++ HandleEvent 调用转换为 JS 函数调用的粘合剂。
因此,在您的情况下,您需要某种侦听器注册/取消注册机制,然后您的实现将触发事件并将它们分派给侦听器。如何完成最后一部分是相当开放的;由于需要实现 DOM 事件规范,Gecko 实现有很多限制,但您应该能够做一些更简单的事情。
SpiderMonkey itself doesn't have anything involving event handling. Events are purely a DOM thing.
The click event is fired by the browser code (the thing embedding SpiderMonkey), not by SpiderMonkey itself. See http://hg.mozilla.org/mozilla-central/file/e60b8be7a97b/content/events/src/nsEventStateManager.cpp for the code that's responsible for dispatching things like click.
The browser is also what defines setter methods that take an assignment to the
onclick
property and turn it into an event listener registration. See http://hg.mozilla.org/mozilla-central/file/e60b8be7a97b/dom/base/nsDOMClassInfo.cpp#l7624 which is called fromnsEventReceiverSH::SetProperty
and handles properties whose name (id
in this code) passes theIsEventName
test.When event listeners are registered and an event is fired, the event dispatcher manages calls to the listeners; the
nsJSEventListener
link you found is the glue that converts a C++HandleEvent
call into a call to a JS function.So in your case, you want some sort of registration/unregistration mechanism for listeners and then your implementation will fire events and dispatch them to listeners. How you do this last part is pretty open-ended; the Gecko implementation has a lot of constraints due to needing to implement the DOM Events specification, but you should be able to do something much simpler.
我相信这是基本概念的完整列表。
I believe this is a full list of basic concepts.