Skip to content

Instantly share code, notes, and snippets.

@sdepold
Forked from 140bytes/LICENSE.txt
Created August 15, 2012 05:50
Show Gist options
  • Save sdepold/3356573 to your computer and use it in GitHub Desktop.
Save sdepold/3356573 to your computer and use it in GitHub Desktop.
140byt.es -- addObserverMethods

addObserverMethods

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).

Usage

  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()
function(
a // an object / instance
) {
a.l = {}; // eventName - listeners - map
a.on = function( // the observer method
b, // the event name
c // the callback
) {
a.l[b] = a.l[b] || []; // init array for current event name
a.l[b].push(c) // save callback for the event name
};
a.fire = function( // the trigger method
b // the event name
) {
// iterate over the event name's callbacks and exec them
(a.l[b] || []).forEach(function(a) {
a()
})
}
}
function(a){a.l={};a.on=function(b,c){a.l[b]=a.l[b]||[];a.l[b].push(c)};a.fire=function(b){(a.l[b]||[]).forEach(function(a){a()})}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Sascha Depold http://depold.com
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "addObserverMethods",
"description": "Bind observer methods to an object.",
"keywords": [
"observer",
"listener"
]
}
<!DOCTYPE html>
<title>addObserverMethods</title>
<script>
var addObserverMethods = function(a){a.l={};a.on=function(b,c){a.l[b]=a.l[b]||[];a.l[b].push(c)};a.fire=function(b){(a.l[b]||[]).forEach(function(a){a()})}}
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()
</script>
@p01
Copy link

p01 commented Aug 15, 2012

Save 23 bytes ;)

function(a,d){a.l={};a.on=a.fire=function(b,c){b=a.l[b]=a.l[b]||[];c?b.push(c):b.forEach(function(a){a()})}}

@sdepold
Copy link
Author

sdepold commented Aug 15, 2012

is there any need for the d ?

@sdepold
Copy link
Author

sdepold commented Aug 15, 2012

b=a.l[b]=a.l[b]||[] he said ... wtf :D

@p01
Copy link

p01 commented Aug 15, 2012

No. Sorry. This was a leftover from another idea I tried.

106 bytes it is then:

function(a){a.l={};a.on=a.fire=function(b,c){b=a.l[b]=a.l[b]||[];c?b.push(c):b.forEach(function(a){a()})}}

104 bytes even ;)

function d(a){a.call?a():a.l={},a.on=a.fire=function(b,c){b=a.l[b]=a.l[b]||[];c?b.push(c):b.forEach(d)}}

@sdepold
Copy link
Author

sdepold commented Aug 15, 2012

:) nice

@WebReflection
Copy link

you can go down to 101 using map rather than forEach ... said that, this will perform quite badly (type change all over) and no "off" is provided

@tsaniel
Copy link

tsaniel commented Aug 16, 2012

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)}}

@tsaniel
Copy link

tsaniel commented Aug 16, 2012

102B : function f(a,b,c){c?a():a.l={},a.on=a.fire=function(d,e){with(a.l[d]=a.l[d]||[])e?push(e):forEach(f)}}

@p01
Copy link

p01 commented Aug 16, 2012

99 bytes:

function f(a,b,c){c?a():c=this.l,c?(a=c[a]=c[a]||[],b?a.push(b):a.forEach(f)):a.l={},a.on=a.fire=f}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment