简体   繁体   English

什么是 initEvents(),我应该在 2018 年使用它吗?

[英]What is initEvents() and should I use it in 2018?

I'm trying to understand the code of another programmer and there is a initEvents () method.我正在尝试理解另一个程序员的代码,并且有一个 initEvents () 方法。 Some articles tells that this is an outdated method, but I'm not sure.有些文章说这是一种过时的方法,但我不确定。 Could you help me?你可以帮帮我吗? What it is?这是什么? And is it really outdated?它真的过时了吗?

This is indeed a deprecated method which will init the event before dispatching , you should instead use the Event constructor which have a second argument for event initialisation and then dispatch the event as you like.这确实是一个不推荐使用的方法,它将init the event before dispatching ,您应该使用Event 构造函数,该构造函数具有用于事件初始化的第二个参数,然后根据需要调度事件。
Something like this:像这样的东西:

// create a look event that bubbles up and cannot be canceled

var evt = new Event("look", {"bubbles":true, "cancelable":false});
document.dispatchEvent(evt);

// event can be dispatched from any element, not only the document
myDiv.dispatchEvent(evt);

initEvents has been deprecated. initEvents已被弃用。

Instead use this to Create Events :而是使用它来创建事件

const customEvent = new Event("CustomEventEmmiter", {"bubbles":true, "cancelable":false});
document.dispatchEvent(customEvent);

And listen to it like this:像这样听它:

window.addEventListener("CustomEventEmmiter", function(){
  alert('CustomEventEmmiter TRIGGERED');
});

Check out more about Events here在此处查看有关活动的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM