gpt4 book ai didi

javascript - 使用 Google Polymer 平台时未定义 _polyfilled?

转载 作者:行者123 更新时间:2023-11-30 17:39:10 24 4
gpt4 key购买 nike

我正在尝试使用 Google Polymer 的 platform.js 创建一个非常简单的示例,但出现以下错误:Uncaught TypeError: Cannot read property '_polyfilled' of undefined

这是我正在尝试做的事情:

<current-date></current-date>

<script>
document.registerElement('current-date', {
prototype: {
createdCallback: function() {
this.innerHTML = new Date();
}
}
});
</script>

http://jsbin.com/uwUxeYA/1/edit

请注意,此代码在未加载 platform.js 的情况下可在 Google Canary 中完美运行。我是否在其他浏览器(最新的 Chrome、Firefox 等)上错误地使用了 polyfill?

最佳答案

您没有正确创建原型(prototype)。在这种形式下,它需要:

document.registerElement('current-date', {
prototype: Object.create(HTMLElement.prototype, {
createdCallback: {
value: function() {
this.innerHTML = new Date();
}
}
})
});

IMO,更易读的形式是:

var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() {
this.innerHTML = new Date();
};
document.registerElement('current-date', {prototype: proto});

工作演示:http://jsbin.com/uwUxeYA/3/edit

关于javascript - 使用 Google Polymer 平台时未定义 _polyfilled?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21411378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com