javascript自定义事件
/** * MicroEvent - to make any js object an event emitter (server or browser) ** - pure javascript - server compatible, browser compatible * - dont rely on the browser doms * - super simple - you get it immediatly, no mistery, no magic involved * * - create a MicroEventDebug with goodies to debug * - make it safer to use*/var MicroEvent= function(){}MicroEvent.prototype= {bind: function(event, fct){this._events = this._events || {};this._events = this._events|| [];this._events.push(fct);},unbind: function(event, fct){this._events = this._events || {};if( event in this._events === false)return;this._events.splice(this._events.indexOf(fct), 1);},trigger: function(event /* , args... */){this._events = this._events || {};if( event in this._events === false)return;for(var i = 0; i < this._events.length; i++){this._events.apply(this, Array.prototype.slice.call(arguments, 1))}}};/** * mixin will delegate all MicroEvent.js function in the destination object * * - require('MicroEvent').mixin(Foobar) will make Foobar able to use MicroEvent * * @param {Object} the object which will support MicroEvent*/MicroEvent.mixin= function(destObject){var props= ['bind', 'unbind', 'trigger'];for(var i = 0; i < props.length; i ++){destObject.prototype]= MicroEvent.prototype];}}// export in common jsif( typeof module !== "undefined" && ('exports' in module)){module.exports= MicroEvent}
页:
[1]