Skip to content

Instantly share code, notes, and snippets.

@dethe
Created August 5, 2015 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dethe/541aa8e33c2eefdd5d1f to your computer and use it in GitHub Desktop.
Save dethe/541aa8e33c2eefdd5d1f to your computer and use it in GitHub Desktop.
(function(global){
'use strict';
var O = function O(defaults){
defaults.__proto__ = this;
this._listeners = {};
return defaults;
}
O.prototype.get = function get(name){
return this[name];
};
O.prototype.set = function set(name, value){
var previous = this[name];
this[name] = value;
if (this._listeners[name]){
this._listeners[name].call(this, {value: value, previous: previous, name: name});
}
};
O.prototype.on = function on(name, listener){
this._listeners[name] = listener;
};
global.O = O;
})(this);
// TESTs
var obj = new this.O({one: 1, two: 2, three: 3});
console.log(obj.get('one') === 1);
console.log(obj.one === 1);
obj.on('two', function(evt){ console.log( evt.value === 'too'); });
obj.set('two', 'too');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment