Created
August 5, 2015 21:30
-
-
Save dethe/541aa8e33c2eefdd5d1f to your computer and use it in GitHub Desktop.
This file contains 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
(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