Why doesn't the shim for watch() and unwatch() at https://gist.github.com/384583 work in phantomjs?

牧云@^-^@ 提交于 2019-12-04 12:43:45

To answer my own question: the provided shim seems to put a broken setter function in place. In particular, unless the handler function returns something meaningful (which it may well not), the property gets set to undefined. Replacing

return newval = handler.call(this, prop, oldval, val);

with

var newval = handler.call(this, prop, oldval, val) || val;
return newval;

seemed to do the job.

As a side note, this particular shim is ingenious but includes a couple of limitations:

  • Code relying on a watched value may take a while to work, so it's good to change watched values after critical operations if using them as flags.

  • You can't add multiple handlers to a value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!