Skip to content

Instantly share code, notes, and snippets.

@tapi
Created February 24, 2012 01:36
Show Gist options
  • Save tapi/1896589 to your computer and use it in GitHub Desktop.
Save tapi/1896589 to your computer and use it in GitHub Desktop.
Quick and dirty Javascript KVO
/* 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