observedAttributes() 不是函数

observedAttributes() is not a function

我在 JS Custom Element

中有属性
  static get observedAttributes() {
    return ['state','color'];
  }

如何在自定义元素的 Callback 函数中获取 整个 数组?

this.observedAttributes() 不是函数

我一定是忽略了一些简单的事情...

在评论中提出问题后更新:

我一直忘记 Getter 和二传手

我现在可以在构造函数中执行此操作():

    this.constructor.observedAttributes.map(attribute => 
    Object.defineProperty(this, attribute, {
      get() {
        return obj.getAttribute(attribute);
      },
      set(newValue) {
        obj.setAttribute(attribute, newValue);
      },
      enumerable: true,//default:false
      configurable: true // default:false
      //writable: true // not valid, since there is a set method!
    }));

(不关心内存消耗的副作用)

observedAttributes 定义为 static,因此它会被 class 调用,而不是实例。 observedAttributes 也是一个 getter (get),所以你不会用 () 来执行它。如果您将自定义元素 class 定义为 FancyButton,您应该使用 FancyButton.observedAttributes.