object.watch

Observe changes on object properties

当前为 2014-09-26 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/5314/18909/objectwatch.js

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址