-
-
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
This file contains hidden or 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
// 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}); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sorry for the question, but how to use? It's need only pure Javascript?