cross-platform-object.watch.js

object.watch polyfill by Eli Grey

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/5908/22033/cross-platform-objectwatchjs.js

  1. /*
  2. * object.watch polyfill
  3. *
  4. * 2012-04-03
  5. *
  6. * By Eli Grey, http://eligrey.com
  7. * Public Domain.
  8. * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  9. */
  10. // object.watch
  11. if (!Object.prototype.watch) {
  12. Object.defineProperty(Object.prototype, "watch", {
  13. enumerable: false
  14. , configurable: true
  15. , writable: false
  16. , value: function (prop, handler) {
  17. var
  18. oldval = this[prop]
  19. , newval = oldval
  20. , getter = function () {
  21. return newval;
  22. }
  23. , setter = function (val) {
  24. oldval = newval;
  25. return newval = handler.call(this, prop, oldval, val);
  26. }
  27. ;
  28. if (delete this[prop]) { // can't watch constants
  29. Object.defineProperty(this, prop, {
  30. get: getter
  31. , set: setter
  32. , enumerable: true
  33. , configurable: true
  34. });
  35. }
  36. }
  37. });
  38. }
  39. // object.unwatch
  40. if (!Object.prototype.unwatch) {
  41. Object.defineProperty(Object.prototype, "unwatch", {
  42. enumerable: false
  43. , configurable: true
  44. , writable: false
  45. , value: function (prop) {
  46. var val = this[prop];
  47. delete this[prop]; // remove accessors
  48. this[prop] = val;
  49. }
  50. });
  51. }