Skip to content

Instantly share code, notes, and snippets.

@XoseLluis
Last active May 15, 2016 02:41
Show Gist options
  • Save XoseLluis/4750176 to your computer and use it in GitHub Desktop.
Save XoseLluis/4750176 to your computer and use it in GitHub Desktop.

Revisions

  1. XoseLluis revised this gist Feb 10, 2013. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion Object.watch.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,13 @@
    // deploytonenyures.blogspot.com
    /*
    Polyfill for the Object.watch/Object.unwatch functions available in Mozilla browsers
    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/watch
    you have a test here:
    http://www.telecable.es/personales/covam1/deployToNenyures/SourceCode/Object.watch.test.js
    and can read more here:
    http://deploytonenyures.blogspot.com.es/2013/02/objectwatch-polyfill.html
    */

    if (!Object.prototype.watch) {
    Object.prototype.watch = function(prop, handler){
  2. XoseLluis revised this gist Feb 10, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Object.watch.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // deploytonenyures.blogspot.com

    if (!Object.prototype.watch) {
    Object.prototype.watch = function(prop, handler){
    var desc = Object.getOwnPropertyDescriptor(this, prop);
  3. XoseLluis created this gist Feb 10, 2013.
    67 changes: 67 additions & 0 deletions Object.watch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    if (!Object.prototype.watch) {
    Object.prototype.watch = function(prop, handler){
    var desc = Object.getOwnPropertyDescriptor(this, prop);
    var newGet;
    var newSet;
    //these cases make little sense, so do nothing we won't be watching readonly descriptors
    if (!desc.configurable
    || (desc.value === undefined && !desc.set)
    || desc.writable === false)
    return;

    if (desc.value){
    var val = desc.value;
    newGet = function(){
    return val;
    };
    newSet = function(newVal){
    val = handler.call(this, prop, val, newVal);
    };
    //let's leverage the setter to store initial information to enable "unwatch"
    newSet._watchHelper = {
    initialType: "dataDescriptor"
    };
    }
    else{
    newGet = desc.get;
    newSet = function(newVal){
    val = handler.call(this, prop, val, newVal);
    desc.set.call(this, val);
    };
    newSet._watchHelper = {
    initialType: "accessorDescriptor",
    oldDesc: desc
    };
    }
    Object.defineProperty(this, prop, {
    get: newGet,
    set: newSet,
    configurable: true,
    enumerable: desc.enumerable
    });
    };

    Object.prototype.unwatch = function(prop){
    var desc = Object.getOwnPropertyDescriptor(this, prop);
    if (desc.set._watchHelper){
    if(desc.set._watchHelper.initialType == "dataDescriptor"){
    Object.defineProperty(this, prop, {
    value: this[prop],
    enumerable: desc.enumerable,
    configurable: true,
    writable: true
    });
    }
    else{
    Object.defineProperty(this, prop, {
    get: desc.get,
    set: desc.set._watchHelper.oldDesc.set,
    enumerable: desc.enumerable,
    configurable: true
    });
    }
    }
    };
    }