javascript - A custom event code -


i have code this:

var eventtarget= function(){     object.defineproperty(this,"__listeners",{         value: {}     }); };  object.defineproperties(eventtarget.prototype,{     addlistener: {         value: function(type,listener){             if(typeof this.__listeners[type]==="undefined"){                 this.__listeners[type]= [];             }              this.__listeners[type].push(listener);         },         enumerable: true     },     __fire: {         value: function(evtobj){              if (typeof evtobj.type==="undefined"){                 throw new error("event needs type");             }              if (typeof evtobj.target==="undefined"){                 evtobj.target = this;             }              var listeners =this.__listeners[evtobj.type];              if (typeof listeners==="undefined"){                 return;             }             (var = 0, len=listeners.length; <len; i++) {                 listeners[i].call(this,evtobj);              }         }     },     removelistener: {         value: function(type,listener){              var listeners = this.__listeners[type];              if(typeof listeners==="undefined"){                 return;             }             (var = 0, len=listeners.length; <len; i++) {                  if (listeners[i]===listener){                     listeners.splice(i,1);                     break;                 }             }         },         enumerable: true     }    });  

it basicaly creates custom events,and calls functions this:

var listener1 =function(evt){             alert(evt.type);         };          var listener2= function(ev){             alert(ev.target);         };          var obj= new eventtarget();         obj.addlistener("load",listener1);         obj.addlistener("load",listener2);          obj.__fire({type: "load"});          obj.removelistener("load",listener1);         obj.removelistener("load",listener2); 

but don t understand few things:

how possible add push method listeners object : this.__listeners[type].push(listener); see checks if property of .__listeners[type] undefined , assigns empty array it.

i don t understand logic behind this. looks me array , object @ same time. tried use this.__listeners[type]= []; without if statement didn t alert load anymore object object

another thing can t understand line of code inside .__fire method purpose of it:

listeners[i].call(this,evtobj); 

and evtobj passed parameter .__fire method? later have var listeners =this.__listeners[evtobj.type]; in case type "load" argument when function called. evt.type used in alert function type of event provided browser? m trying understand code little difficult because dont have programming background ve learned tutorials basics , continued more complex code.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -