search
HomeWeb Front-endJS TutorialOverview of js custom events and event interaction principles (2)_javascript skills

The purpose of js custom events (1) is just to let everyone simply understand how custom events are simulated. It is not difficult to find that there will be many defects, such as:
1. This event object can only register one event. Provide multiple events
2. The registration method does not return some information

Let’s solve these problems below. The following is the source code of MyEvent.js:

Copy code The code is as follows:

function MyEvent() {
this.handlers={};
}
MyEvent.prototype={
addHandler:function(type,handler)
{
if(typeof this.handlers[type] == "undefined")
{
this.handlers[type]=[];
}
this.handlers[type].push(handler);
},
fire:function(event)
{if(this.handlers[event.type] instanceof Array)
{
var handlers=this.handlers[event.type];
for(var i= 0, len=handlers.length;i{
handlers[i](event);
}
}
},
removeHandler:function(type , handler)
{
if(this.handlers[type] instanceof Array)
{
var handlers=this.handlers[type];
for(var i= 0, len= handlers.length;i{
if(handlers[i]===handler)
{
break;
}
}
handlers. splice(i,1);
}
}
};

This type is the optimization of the first article.
The attribute handler becomes handlers and becomes an array.
The addHandler() method accepts two parameters: the event type and the function used to handle the event. When this method is called, a check will be made to see if there is already an array
for this event type in the handlers attribute; if not, a new one will be created. Then use push() to add that handler to the end of the array.

The fire() method is used to trigger an event. This method accepts a parameter, which is an object containing at least the type attribute. Otherwise, it cannot be determined whether the handlers already exist. It will use this type to find a set of handlers corresponding to the event type, call each function, and give the event object. Because these are custom objects, other things on the event object can be defined by you.

The removeHandler() method is an auxiliary to addHandler(). They accept the same parameters: the type of event and the event handler. This method searches the array of event handlers to find the location of the handler to be removed. If found, use the break operator to exit the loop. The item is then removed from the array using the splice() method.

Let’s change the usage method here to a longer-used form. Now according to my knowledge, many products have two ways to use events. One is direct inheritance (js does not have this concept in this province, but we can By simulating the effect of inheritance through the prototype chain (not explained in detail here), this event object will have these behaviors, but this method is more limited. Another way is more common, which is to create it on the class using the event. A property to hold this object. For example: create a Student.js file in the same directory, the code inside is as follows:
Copy the code The code is as follows:

function Student(name)
{
this.myEvent=new MyEvent();
this.name=name;
}
Student.prototype={
setName:function(name)
{
var eventStart={
type:"changeNameStart",
newName:name,
oldName:this.name
};
this.myEvent.fire(eventStart);
this.name=name;
}
}

There is a student class here, and a MyEvent object is initialized in the constructor as Attribute, initialize the name attribute through parameters.

Provides a method setName for changing the name, but before changing the name, a listener that may trigger the event changeNameStart is set.
Create an html page and place it in the same directory. The code is as follows:
Copy the code The code is as follows:












This will be very convenient to use and is also a common way of use.
Generally when using events in real projects, we still need to do some optimization, such as:
1. Users do not know what events we provide. From the code, it seems that any event can be added to handlers. , but the event that really takes effect (where we set the fire() method) cannot be seen intuitively from the code. Generally, when making products, we need to consider this aspect.

2. Did you find that the parameter event of fire does not seem to be fixed. In the Daxing project, it is best to make the event a type. It is more convenient to use in the fire place. There may be many kinds of events. Type, then there may be some judgments in fire.
I hope it will be helpful to readers who are new to js events and can communicate with each other.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
打开win11的分屏交互方式打开win11的分屏交互方式Dec 25, 2023 pm 03:05 PM

在win11系统中,我们可以通过打开分屏交互来让多个显示器使用同一款系统,共同操作,但是很多朋友不知道分屏交互怎么开启,其实只要在系统设置中找到显示器就可以了,下面一起来学习一下吧。win11分屏交互怎么打开1、点击开始菜单,找到其中的“设置”2、然后在其中找到“系统”设置。3、进入系统设置后,在左侧选择“显示”4、接着在右边的多显示器中选择“扩展这些显示器”即可。

Vue3+TS+Vite开发技巧:如何与后端API进行交互Vue3+TS+Vite开发技巧:如何与后端API进行交互Sep 08, 2023 pm 06:01 PM

Vue3+TS+Vite开发技巧:如何与后端API进行交互引言:在网页应用开发中,前端与后端之间的数据交互是一个非常重要的环节。Vue3作为一种流行的前端框架,与后端API进行交互的方式也有很多种。本文将介绍如何使用Vue3+TypeScript+Vite开发环境来与后端API进行交互,并通过代码示例来加深理解。一、使用Axios发送请求Axios是

VUE3基础教程:使用Vue.js自定义事件VUE3基础教程:使用Vue.js自定义事件Jun 15, 2023 pm 09:43 PM

Vue.js是一款流行的JavaScript框架,它提供了很多方便的特性,所以它在开发Web应用程序时非常有用。Vue.js中的自定义事件系统使其更加灵活,并且可以通过组件事件触发和处理来实现更好的代码重用性。在本文中,我们将讨论如何使用Vue.js的自定义事件。Vue.js中自定义事件的基础在Vue.js中,我们可以通过v-on指令来监听DOM事件。例如,

uniapp实现如何使用JSBridge实现与原生交互uniapp实现如何使用JSBridge实现与原生交互Oct 20, 2023 am 08:44 AM

uniapp实现如何使用JSBridge实现与原生交互,需要具体代码示例一、背景介绍在移动应用开发中,有时需要与原生环境进行交互,比如调用原生的一些功能或获取原生的一些数据。uniapp作为一种跨平台的移动应用开发框架,提供了一种方便的方式来实现与原生交互,即使用JSBridge进行通信。JSBridge是一种前端与移动原生端进行交互的技术方案,通过在前端和

PHP与JavaScript交互的方法及常见问题解答PHP与JavaScript交互的方法及常见问题解答Jun 08, 2023 am 11:33 AM

PHP与JavaScript交互的方法及常见问题解答随着互联网的快速发展,网页已经成为人们获取信息、进行交流的主要平台。而PHP和JavaScript是开发网页的两种最常用语言。它们都具有各自的优点和适用场景,而在大型网站的开发过程中,两者的结合将会拓展开发人员的的工作能力。本文将介绍PHP和JavaScript交互的方法及常见问题解答。PHP与JavaSc

前端后端开发的发展历程与趋势展望前端后端开发的发展历程与趋势展望Mar 26, 2024 am 08:03 AM

随着互联网的迅猛发展和信息技术的日新月异,前端和后端开发作为两个重要的IT领域在过去几十年中也取得了巨大的进步。本文将探讨前端后端开发的发展历程,分析当前的发展趋势,并展望未来的发展方向。一、前端后端开发的发展历程早期阶段在互联网刚刚兴起的时期,网站开发主要关注内容的呈现,前端开发工作主要集中在HTML、CSS和JavaScript等技术上,以实现页面的基本

使用企业微信接口与PHP进行数据交互的方法使用企业微信接口与PHP进行数据交互的方法Jul 05, 2023 am 09:00 AM

使用企业微信接口与PHP进行数据交互的方法企业微信是企业内部沟通和协作的重要平台,开发者可以通过企业微信接口实现与企业微信的数据交互。本文将介绍如何使用PHP语言来调用企业微信接口,实现数据的传输与处理。首先,需要创建一个企业微信应用,并获取相应的CorpID、Secret以及AgentID。这些信息可以在企业微信管理后台的“应用与小程序”中找到。接下来,我

Vue自定义事件的用法和常见场景Vue自定义事件的用法和常见场景Sep 15, 2023 pm 12:12 PM

Vue自定义事件的用法和常见场景Vue.js是一个流行的JavaScript框架,用于构建用户界面。在Vue中,我们可以通过自定义事件来实现组件之间的通信。自定义事件是Vue中非常有用的功能之一,可以让我们在不同的组件之间传递数据和触发特定的行为。本文将介绍Vue中自定义事件的用法和常见场景,并提供具体的代码示例。一、自定义事件的基本用法在Vue中,我们可以

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment