JS can also have custom event injection it's so cool.

Source: Internet
Author: User
Tags implement one more line
Js

There are delegate in C #, and there are special delegate that can be applied directly to event programming, that is event. In JS, there is no C # event, there is no delegate, there is only a DOM element built-in native of the event, such as unable to add events for the input element, only in its own events (such as Onclick=handler) on the extension of the application. So can you do a custom event simulation effect? The answer is yes, the subject of this article.
First, figure out the intent of the event--you can execute extra code when something happens, such as document.attachevent (' onclick ', function () {alert (' u click Document ')}), When the page is clicked (the event occurs), it executes the other code we hook for it (in JS, a function is the set of statements, hereinafter called function), and of course we can hook up any number of function on an event, thus implementing a flexible extensible programming interface. Imagine that if you can extend an arbitrary method of any object in the same way as an element event extension application, it will be more flexible for JS programming. To take a look at an example, we usually name a function of relative opposition and call it where needed (usually another function) to implement code reuse:
function F () {
This.method = function () {
Alert (' F.method is called ')
g ();
}
}
function g () {
Alert (123)
}
var F = new F ();
F.method ()


We make a direct call to G in F.method, encapsulated in an Event object to achieve the same effect, the code is as follows:
var Event = {
    __list:[],
    observe:function (obj, Ev, fun) {
        This.__list.push ( {o:obj, E:ev, F:fun})
   },
    occor:function (obj, method) {
         var arr = []
        for (var i=0; i<this.__list.length; i++) {
            if (this.__list[i].o==obj && this._ _list[i].e==method) Arr.push (This.__list[i]);
       }
        for (var i=0; i<arr.length; i++) {
             arr[i].f ();
       }
   }
}

function F () {
This.method = function () {
Alert (' F.method is called ')
Event.occor (This, ' method ');
}
}

var F = new F ();
Event.observe (F, ' method ', function () {alert (123)})
F.method () At first glance seems to cost "too much", but it's more generic to "call G in F", and if you want to call H in f you just need one more line Event.occor (this, ' MethodName '), Write here you must also notice that MethodName's writing is the same as the first, is not flexible, if the method of each class is written Event.occor (this, ' methods ') is too unsightly, also deviate from our original intention, Dynamically modify method to add it to the last line OK, the next step is to solve it, improve the code as follows:

var Event = {
__list:[],
Observe:function (obj, Ev, fun) {
This.__list.push ({o:obj, E:ev, F:fun})
},
Occor:function (obj, method) {
var arr = []
for (var i=0; i<this.__list.length; i++) {
if (this.__list[i].o==obj && this.__list[i].e==method) Arr.push (this.__list[i));
}
for (var i=0; i<arr.length; i++) {
ARR[I].F ();
}
},
Inject:function (obj) {
For (var p in obj) {
OBJ[P] = new Function (obj[p].tostring (). Replace (' Function () {', '). Replace ('} ', ' Event.occor (this,p) '))
}
}
}

function F () {
This.method = function () {
Alert (' F.method is called ')
}
}

var F = new F ();
Event.inject (f);
Event.observe (F, ' method ', function () {alert (123)})
F.method () We rewrite the displayed Event.occor to Event.inject in the called Method body. Here we are simple (and there are some security code not to be processed, such as not to determine whether obj[p] needs to be rewritten, no test efficiency issues, There is no logical judgment to handle more event.occor, and the next step is to implement it as a Observeable object, more flexible, and complete the custom event.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.