Advertisement
Guest User

Untitled

a guest
May 29th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. if (!Object.prototype.watch) {
  2. Object.defineProperty(Object.prototype, "watch", {
  3. enumerable: false,
  4. configurable: true,
  5. writable: false,
  6. value: function (prop, handler) {
  7. var oldval = this[prop],
  8. newval = oldval,
  9. getter = function () {
  10. return newval;
  11. },
  12. setter = function (val) {
  13. oldval = newval;
  14. return newval = handler.call(this, prop, oldval, val);
  15. };
  16.  
  17. if (delete this[prop]) { // can't watch constants
  18. Object.defineProperty(this, prop, {
  19. get: getter,
  20. set: setter,
  21. enumerable: true,
  22. configurable: true
  23. });
  24. }
  25. }
  26. });
  27. }
  28.  
  29. if (!Object.prototype.unwatch) {
  30. Object.defineProperty(Object.prototype, "unwatch", {
  31. enumerable: false,
  32. configurable: true,
  33. writable: false,
  34. value: function (prop) {
  35. var val = this[prop];
  36. delete this[prop]; // remove accessors
  37. this[prop] = val;
  38. }
  39. });
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement