event.initEvent 对比 event.initCustomEvent

event.initEvent vs event.initCustomEvent

我正在处理与 Custom Events 相关的任务,我想知道这些方法之间是否存在差异 除了 event data[ 的额外参数外=21=]

MDN - The old-fashioned way uses event.initEvent but the polyfill on MDN - Cutom Events 使用 event.initCustomEvent 初始化事件。

我参考了Passing additional arguments in custom Event,但我们甚至可以使用event.details来设置event data

JSFiddle - Event data with initEvent

所以问题是,使用其中的特定一个有什么好处吗?

来自 DOM 规范:

The initCustomEvent(type, bubbles, cancelable, detail) method must, when invoked, run these steps:

  1. If context object's dispatch flag is set, terminate these steps.
  2. Initialize the context object with type, bubbles, and cancelable.
  3. Set context object's detail attribute to detail.

The initEvent(type, bubbles, cancelable) method, when invoked, must run these steps:

  1. If context object's dispatch flag is set, terminate these steps.
  2. Initialize the context object with type, bubbles, and cancelable.

Note: As events have constructors initEvent() is superfluous. However, it has to be supported for legacy content.

因此,除了 detail 参数之外,它们是相同的。

event.initEvent 此功能已从 Web 标准中删除。虽然某些浏览器可能仍然支持它,但它正在被删除。不要在旧项目或新项目中使用它。使用它的页面或 Web 应用程序可能随时崩溃。 MDN