是否可以在常规对象(而非 DOM 对象)上分派事件?
我刚刚发现 FileReader 调度事件就像它是 DOM 元素一样。是吗?我想知道是否可以创建一个类似于 FileReader 的对象,它没有 HTML/XML 结构的表示,但可以调度事件?
I just found out that FileReader dispatches events just as if it was a DOM element. Is it? I wonder if it's possible to create an object similar to FileReader, which doesn't have a representation in HTML/XML structure, but can dispatch events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
FileReader
具有类似于addEventHandler
的方法,因为它是定义的 实现EventTarget
接口。EventTarget
由 DOM Events 规范定义,但您不这样做不需要是 DOM 对象来实现它。window
、XMLHttpRequest
和FileReader
是实现EventTarget
的其他浏览器对象模型对象。不幸的是,没有简单的方法可以利用浏览器的事件目标本机实现...您可以尝试通过使用浏览器对象作为
prototype
属性来继承,但这通常非常不可靠。然而,用纯 JavaScript 编写代码来实现所有方法并不是太困难:注意:上面的代码是我的想法,未经测试,但可能有效。但是它有一些限制,例如如果
Event
对象支持 DOM Level 3defaultPrevented
属性,则仅支持dispatchEvent
返回值,并且不支持 DOM Level 3stopImmediatePropagation()
(这是不可能实现的,除非您依赖于自己公开其属性的 Event 对象)。此外,没有层次结构或捕获/冒泡的实现。所以 IMO:尝试编写参与 DOM 事件模型的代码并不会获得太多好处。对于纯 JS 回调工作,我只需使用您自己的侦听器列表的临时实现。
FileReader
has methods likeaddEventHandler
because it is defined to implement theEventTarget
interface.EventTarget
is defined by the DOM Events spec but you don't need to be a DOM object to implement it.window
,XMLHttpRequest
andFileReader
are other Browser Object Model objects that implementEventTarget
.Unfortunately there's no easy way to piggyback on the browser's native implementation of event targets... you could try inheriting from a browser object by using one as a
prototype
property, but that's very unreliable in general. However it is not too difficult to write code to implement all the methods yourself in plain JavaScript:Caution: the above code is off the top of my head and untested, but may work. However it has limitations like only supporting the
dispatchEvent
return value if theEvent
object supports the DOM Level 3defaultPrevented
property, and no support for DOM Level 3stopImmediatePropagation()
(which is impossible to implement unless you rely on your own Event object that exposes a property for it). Also there's no implementation of hierarchy or capture/bubbling.So IMO: you don't gain much by trying to write code that participates in the DOM Events model. For plain-JS callback work I'd just go with your own ad hoc implementation of listener-lists.
bobince 的想法是正确的,但他的代码只是一个示例。对于经过实际考验的实现,Mr. Doob 在 Three.js 中使用了一个。
bobince has the right idea, but his code is just an example. For an actual battle-tested implementation, Mr. Doob has one that he uses in three.js.
jQuery 可以从常规对象调度事件。 这是一个小提琴。
jQuery can dispatch events from regular objects. Here's a fiddle.
我认为它是 javascript ;通常,任何可以获得 DOM 元素引用的对象都应该能够使用 element.dispatchEvent 函数调度事件;请参阅:
https://developer.mozilla.org/en/DOM/document.createEvent
https://developer.mozilla.org/en/DOM/element.dispatchEvent
I assume it's javascript ; normally any object that can get a reference to a DOM element should be able to dispatch an event using the element.dispatchEvent function ; see :
https://developer.mozilla.org/en/DOM/document.createEvent
https://developer.mozilla.org/en/DOM/element.dispatchEvent