asteroids-listener
Advanced tools
+1
-1
| { | ||
| "name": "asteroids-listener", | ||
| "version": "0.0.0", | ||
| "version": "0.0.1", | ||
| "description": "Custom implementation of the observer pattern tailored for Asteroids", | ||
@@ -5,0 +5,0 @@ "main": "lib/listener.js", |
+54
-0
@@ -5,1 +5,55 @@ asteroids-listener | ||
| Custom implementation of the observer pattern tailored for Asteroids | ||
| Examples | ||
| -------- | ||
| ### Direct | ||
| ```javascript | ||
| var Listener = require('asteroids-listener'); | ||
| var listener = new Listener(); | ||
| var identifier = listener.addListener('change', function(){ | ||
| console.log('a change occured'); | ||
| }) | ||
| listener.notifyOf('change'); | ||
| listener.removeListener(identifier); | ||
| listener.notifyOf('change'); | ||
| ``` | ||
| Output: | ||
| ``` | ||
| a change occured | ||
| ``` | ||
| ### Prototype | ||
| ```javascript | ||
| var Listener = require('asteroids-listener'); | ||
| var Discussion = function(){ | ||
| Listener.call(this); | ||
| } | ||
| Discussion.prototype = new Listener(); | ||
| Discussion.prototype.agreement = function(){ | ||
| this.notifyOf('agreement'); | ||
| }; | ||
| var discussion = new Discussion(); | ||
| discussion.addListener('agreement', function Stakeholder(){ | ||
| console.log('celebrating'); | ||
| }) | ||
| discussion.agreement(); | ||
| ``` | ||
| Output: | ||
| ``` | ||
| celebrating | ||
| ``` |
4887
19.69%59
1080%