Skip to content

Instantly share code, notes, and snippets.

@SerzN1
Forked from eligrey/object-watch.js
Created July 5, 2016 19:23
Show Gist options
  • Save SerzN1/e0c6fd05490e519d02da0d12de4db03f to your computer and use it in GitHub Desktop.
Save SerzN1/e0c6fd05490e519d02da0d12de4db03f to your computer and use it in GitHub Desktop.

Revisions

  1. @eligrey eligrey revised this gist May 1, 2012. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions object-watch.js
    Original file line number Diff line number Diff line change
    @@ -15,20 +15,24 @@ if (!Object.prototype.watch) {
    , configurable: true
    , writable: false
    , value: function (prop, handler) {
    var oldval = this[prop], newval = oldval,
    getter = function () {
    var
    oldval = this[prop]
    , newval = oldval
    , getter = function () {
    return newval;
    },
    setter = function (val) {
    }
    , setter = function (val) {
    oldval = newval;
    return newval = handler.call(this, prop, oldval, val);
    };
    }
    ;

    if (delete this[prop]) { // can't watch constants
    Object.defineProperty(this, prop, {
    get: getter,
    set: setter,
    enumerable: true,
    configurable: true
    get: getter
    , set: setter
    , enumerable: true
    , configurable: true
    });
    }
    }
  2. @eligrey eligrey revised this gist Apr 3, 2012. 1 changed file with 35 additions and 22 deletions.
    57 changes: 35 additions & 22 deletions object-watch.js
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,50 @@
    // Cross-browser object.watch and object.unwatch
    /*
    * object.watch polyfill
    *
    * 2012-04-03
    *
    * By Eli Grey, http://eligrey.com
    * Public Domain.
    * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
    */

    // object.watch
    if (!Object.prototype.watch) {
    Object.prototype.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(Object.prototype, "watch", {
    enumerable: false
    , configurable: true
    , writable: false
    , value: 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
    Object.defineProperty(this, prop, {
    get: getter,
    set: setter,
    enumerable: false,
    enumerable: true,
    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.unwatch
    if (!Object.prototype.unwatch) {
    Object.prototype.unwatch = function (prop) {
    var val = this[prop];
    delete this[prop]; // remove accessors
    this[prop] = val;
    };
    Object.defineProperty(Object.prototype, "unwatch", {
    enumerable: false
    , configurable: true
    , writable: false
    , value: function (prop) {
    var val = this[prop];
    delete this[prop]; // remove accessors
    this[prop] = val;
    }
    });
    }
  3. @eligrey eligrey revised this gist Sep 10, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion object-watch.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ if (!Object.prototype.watch) {
    Object.defineProperty(this, prop, {
    get: getter,
    set: setter,
    enumerable: true,
    enumerable: false,
    configurable: true
    });
    } else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
  4. @eligrey eligrey revised this gist Apr 30, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions object-watch.js
    Original file line number Diff line number Diff line change
    @@ -12,14 +12,14 @@ if (!Object.prototype.watch) {
    return newval = handler.call(this, prop, oldval, val);
    };
    if (delete this[prop]) { // can't watch constants
    if (Object.defineProperty) // ECMAScript 5
    if (Object.defineProperty) { // ECMAScript 5
    Object.defineProperty(this, prop, {
    get: getter,
    set: setter,
    enumerable: true,
    configurable: true
    });
    else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
    } else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
    Object.prototype.__defineGetter__.call(this, prop, getter);
    Object.prototype.__defineSetter__.call(this, prop, setter);
    }
  5. @eligrey eligrey created this gist Apr 30, 2010.
    37 changes: 37 additions & 0 deletions object-watch.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // Cross-browser object.watch and object.unwatch

    // object.watch
    if (!Object.prototype.watch) {
    Object.prototype.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: true,
    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.unwatch
    if (!Object.prototype.unwatch) {
    Object.prototype.unwatch = function (prop) {
    var val = this[prop];
    delete this[prop]; // remove accessors
    this[prop] = val;
    };
    }