The examples in this article describe the usage of custom events in JavaScript. Share it with everyone for your reference. The specific analysis is as follows:
In web front-end development, many people may not use custom events of js, but if you are doing a relatively large project, especially when multiple people are developing collaboratively, custom events are very important. It's important. So, what are custom events in js? Let’s look at an example first:
Front-end developer A encapsulates a function:
function move(){
alert(a); //This represents N lines of code
}
After a while, front-end developer B will enrich this function based on A, so he will write like this:
function move(){
alert(a); //This represents N lines of code
alert(b); //This represents N lines of code
}
Did you find a problem? B should pay attention to naming and conflict issues with A's variables, functions, etc. After a while, front-end developer C will also enrich this function, so:
function move(){
alert(a); //This represents N lines of code
alert(b); //This represents N lines of code
alert(c); //This represents N lines of code
}
It will be very frustrating at this time, and I am sure it will not be easy to write code in C. The way to solve this problem is to use custom events. We know that the same event can be added to an element without affecting each other, such as:
window.addEventListener('click',function(){
alert(1);
} ,false);
window.addEventListener('click',function(){
alert(2);
} ,false);
When clicking on the page, both 1 and 2 will pop up, then we can use this method to define our function:
window.addEventListener('move',function(){
alert(3);
} ,false);
window.addEventListener('move',function(){
alert(4);
} ,false);
In this way, when we execute move();, 3 and 4 will pop up. The move here is a custom event, which is actually a function
Let’s see how to pass parameters to the event handler:
//Encapsulate a function with parameters into a function without parameters
function createFunction(obj, strFunc) {
var args = []; //Define args to store the parameters passed to the event handler
If (!obj) obj = window; //If it is a global function, obj=window;
//Get the parameters passed to the event handler
for (var i = 2; i < arguments.length; i ) args.push(arguments[i]);
//Use a parameterless function to encapsulate the call of the event handler
Return function() {
obj[strFunc].apply(obj, args); // Pass parameters to the specified event handler
}
}
function class1() {
}
class1.prototype = {
show: function() {
This.onShow();
},
onShow: function() { }
}
function objOnShow(userName) {
alert("hello," userName);
}
function test() {
var obj = new class1();
var userName = "test";
obj.onShow = createFunction(null, "objOnShow", userName);
Obj.show();
}
"Because the event mechanism only passes the name of a function without any parameter information, parameters cannot be passed in." This is something for later. "To solve this problem, you can think about it from the opposite way. No Think about how to pass parameters in, but consider how to build an event handler without parameters. This program is created based on the event handler with parameters and is an outer package. "The "program" here is createFunction. function, which cleverly uses the apply function to encapsulate functions with parameters into parameterless functions. Finally, let’s take a look at how to implement multiple bindings of custom events:
// Make custom events support multiple bindings
//Encapsulate functions with parameters into functions without parameters
function createFunction(obj, strFunc) {
var args = []; //Define args to store the parameters passed to the event handler
If (!obj) obj = window; //If it is a global function, obj=window;
//Get the parameters passed to the event handler
for (var i = 2; i < arguments.length; i ) args.push(arguments[i]);
//Use a parameterless function to encapsulate the call of the event handler
Return function() {
obj[strFunc].apply(obj, args); // Pass parameters to the specified event handler
}
}
function class1() {
}
class1.prototype = {
show: function() {
if (this.onShow) {
for (var i = 0; i < this.onShow.length; i ) {
This.onShow[i]();
}
}
},
attachOnShow: function(_eHandler) {
If (!this.onShow) { this.onShow = []; }
This.onShow.push(_eHandler);
}
}
function objOnShow(userName) {
alert("hello," userName);
}
function objOnShow2(testName) {
alert("show:" testName);
}
function test() {
var obj = new class1();
var userName = "your name";
Obj.attachOnShow(createFunction(null, "objOnShow", userName));
Obj.attachOnShow(createFunction(null, "objOnShow2", "test message"));
Obj.show();
}
We see that the basic idea of the attachOnShow method is to push the array. In fact, we can also remove the event processing function after the event execution is completed. Implement it separately below:
//Encapsulate functions with parameters into functions without parameters
function createFunction(obj, strFunc) {
var args = []; //Define args to store the parameters passed to the event handler
If (!obj) obj = window; //If it is a global function, obj=window;
//Get the parameters passed to the event handler
for (var i = 2; i < arguments.length; i ) args.push(arguments[i]);
//Use a parameterless function to encapsulate the call of the event handler
Return function() {
obj[strFunc].apply(obj, args); // Pass parameters to the specified event handler
}
}
function class1() {
}
class1.prototype = {
show: function() {
if (this.onShow) {
for (var i = 0; i < this.onShow.length; i ) {
This.onShow[i]();
}
}
},
attachOnShow: function(_eHandler) { // Attach event
If (!this.onShow) { this.onShow = []; }
This.onShow.push(_eHandler);
},
detachOnShow: function(_eHandler) { // Remove event
If (!this.onShow) { this.onShow = []; }
This.onShow.pop(_eHandler);
}
}
function objOnShow(userName) {
alert("hello," userName);
}
function objOnShow2(testName) {
alert("show:" testName);
}
function test() {
var obj = new class1();
var userName = "your name";
Obj.attachOnShow(createFunction(null, "objOnShow", userName));
Obj.attachOnShow(createFunction(null, "objOnShow2", "test message"));
Obj.show();
obj.detachOnShow(createFunction(null, "objOnShow", userName));
Obj.show(); // Remove one and display the remaining one
obj.detachOnShow(createFunction(null, "objOnShow2", "test message"));
Obj.show(); // Remove both and display neither
}
I hope this article will be helpful to everyone’s JavaScript programming design.