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()
No. Sorry. This was a leftover from another idea I tried.
106 bytes it is then:
104 bytes even ;)