Created
November 5, 2010 17:31
-
-
Save ollym/664490 to your computer and use it in GitHub Desktop.
Simple ES5 Class Inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Event Emitter Class | |
// ringo/events.js | |
export('EventEmitter'); | |
function EventEmitter() { | |
} | |
EventEmitter.prototype = Object.create(Object.prototype, | |
event: { | |
value: {} | |
}, | |
on: { | |
value: function(event, callback) { | |
if (event in this.events) { | |
this.events[event] = []; | |
} | |
this.events[event].push(callback); | |
} | |
}, | |
emit: { | |
value: function(event, args) { | |
if ( ! (event in this.events)) { | |
return; | |
} | |
this.events[event].forEach(function(callback) { | |
callback.apply(undefined, args); | |
}); | |
} | |
} | |
} | |
// My Observable Class | |
// Implements the event emitter interface | |
{EventEmitter} = require('ringo/events'); | |
export('MyClass'); | |
function MyClass() { | |
} | |
MyClass.prototype = Object.create(EventEmitter.prototype, { | |
myValue: { value: 'abc' } | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment