Skip to content

Instantly share code, notes, and snippets.

@ericcholis
Forked from eligrey/object-watch.js
Created October 27, 2011 13:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericcholis/1319481 to your computer and use it in GitHub Desktop.
Save ericcholis/1319481 to your computer and use it in GitHub Desktop.
Cross-browser object.watch and unwatch. Modified for use with MooTools and MooTools More
// Cross-browser object.watch and object.unwatch
// Requires Mootools 1.4 or greater
// object.watch
(function(){
if (!Object.prototype.watch) {
Object.implement('watch',function(prop,handler){
var oldval = this[prop], newval = oldval,
getter = function () {
return newval;
},
setter = function (val) {
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
};
if (delete this[prop]) { // can't watch constants
if (Object.defineProperty) { // ECMAScript 5
Object.defineProperty(this, prop, {
get: getter,
set: setter,
enumerable: false,
configurable: true
});
} else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
Object.prototype.__defineGetter__.call(this, prop, getter);
Object.prototype.__defineSetter__.call(this, prop, setter);
}
}
});
Object.prototype.watch.extend('include',function(){return});
Object.prototype.watch.extend('split',function(){return});
};
// object.unwatch
if (!Object.prototype.unwatch) {
Object.implement('unwatch',function (prop) {
var val = this[prop];
delete this[prop]; // remove accessors
this[prop] = val;
});
Object.prototype.unwatch.extend('include',function(){return});
Object.prototype.unwatch.extend('split',function(){return});
};
})();
@edineibauer
Copy link

sorry for the question, but how to use? It's need only pure Javascript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment