function mixin(receiver, supplier) {
for (var property in supplier) {
if (supplier.hasOwnProperty(property)) {
receiver[property] = supplier[property];
}
}
}
function EventTarget() {
}
EventTarget.prototype = {
constructor: EventTarget,
addListener: function (type, listener) {
if (!this.hasOwnProperty('_listeners')) {
this._listeners = [];
}
if (typeof this._listeners[type]=="undefined") {
this._listeners[type] = [];
}
this._listeners[type].push(listener);
},
fire: function (event) {
if (!event.target) {
event.target = this;
}
if (!event.type) {
throw new Error('event.type missing');
}
if (this._listeners && this._listeners[event.type] instanceof Array) {
var listeners = this._listeners[event.type];
for (var i = 0, len = listeners.length; i < len; i++) {
listeners[i].call(this, event);
}
}
},
removeListener: function (type, listener) {
if (this._listeners && Array.isArray(this._listeners[type])) {
var listeners = this._listeners[type];
for (var i = 0, len = listeners.length; i < len; i++) {
if (listeners[i] === listener) {
listeners.splice(i, 1);
return;
}
}
}
}
}
var target = new EventTarget();
target.addListener("message", function (event) {
console.info("Message is "+event.data);
});
target.fire( {
'type':'message',
'data':'Kong desong'
});
console