Skip to content

Instantly share code, notes, and snippets.

@ollym
Created November 5, 2010 17:31
Show Gist options
  • Save ollym/664490 to your computer and use it in GitHub Desktop.
Save ollym/664490 to your computer and use it in GitHub Desktop.
Simple ES5 Class Inheritance
// 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