使用变更观察程序来观察Object.obsobe()下的本机属性

Using change watchers to observe native properties under Object.observe()

本文关键字:观察 属性 本机 obsobe Object 程序      更新时间:2023-09-26

既然Object.observe()在Chrome中默认打开,我遇到了很多情况,我想重用浏览器的内置属性(hiddentitledraggable),但当属性更改时,*Changed观察者将不再被调用。

一个例子是hidden:http://jsbin.com/jizikaje/1/edit(从未调用hiddenChanged()

我目前的解决方法是使用attributeChanged()来观察属性的变化:

attributeChanged: function(attrName, oldVal, newVal) {
  // Cannot use *Changed watchers for these native properties.
  if (attrName == 'hidden') {
    this.marker.setVisible(!this.hidden);
  }
}

建议的方法是什么?


顺便说一句,在尝试使用本机属性时抛出警告将花费很长时间进行调试:https://github.com/Polymer/polymer/issues/379

Object.observe()用于JavaScript内容,MutationObserver用于DOM元素。在这种情况下,MutationObserver可能会更好地工作。

这似乎是Polymer 1的旧帖子,可能不再是一个有用的问题。

Object.obsote()提议已被撤回,任何浏览器都不支持它。

聚合物现在使用支持观察者的声明特性。