The function adds everything that is needed to an object. It will get a event-callbacks-map, an observer/listener method (on
)
and a trigger method (fire
).
var obj = {
walk: function() {
// do smth
this.fire('walk')
}
}
addObserverMethods(obj)
obj.on('walk', function() {
alert('it moved!')
})
obj.walk()
Or use it with a prototype:
var Player = function(){
addObserverMethods(this)
}
Player.prototype.walk = function() {
// do smth
this.fire('walk')
}
var player = new Player()
player.on('walk', function() {
alert('it moved!')
})
player.walk()
103 bytes:
function d(a){a.call?a():a.l={},a.on=a.fire=function(b,c){with(a.l[b]=a.l[b]||[])c?push(c):forEach(d)}}