1

在 Mozilla 开发者网络上,有一个关于如何使用 MutationObserver的示例。

// select the target node
var target = document.querySelector('#some-id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.type);
  });    
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
observer.disconnect();

config变量是Object一个。但是闭包编译器会生气

错误 - MutationObserver.prototype.observe 的实际参数 2 与找到的形式参数不匹配
:{attributes: boolean, characterData: boolean, childList: boolean}
required: (MutationObserverInit|null|undefined)observer.observe(node, config);

因为它是在externs/html5.js中设置的。
但是,我找不到实例化 MutationObserverInit 类型对象的方法,因为“未捕获的 ReferenceError:未定义 MutationObserverInit”。

在这里做什么是正确的?

4

1 回答 1

3

我认为外部文件是错误的,应该修复。

这是解决方法:

var config = /** @type {MutationObserverInit} */ ({ attributes: true, childList: true, characterData: true });
于 2015-04-30T10:06:03.753 回答