Created
February 24, 2012 01:36
-
-
Save tapi/1896589 to your computer and use it in GitHub Desktop.
Quick and dirty Javascript KVO
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
/* Extends the Class Object Written by Resig. http://ejohn.org/blog/simple-javascript-inheritance/ */ | |
Class.prototype.addObserver = function(/** String */ key, /** Object */ observer, /** Function */ method) | |
{ | |
if (key !== null && key !== undefined && | |
observer !== null && observer !== undefined && | |
method !== null && method !== undefined) | |
{ | |
eval("this._" + key + " = this." + key); | |
this.__defineGetter__(key, function() | |
{ | |
return eval("this._" + key); | |
}); | |
this.__defineSetter__(key, function(value) | |
{ | |
eval("this._" + key + " = value"); | |
method.apply(observer); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment