如何使用 JavaScript get 和 set 进行 polyfill
我正在尝试弄清楚如何为 数据集 制作一个polyfill,我认为我需要使用 get 和 set 来定义函数。当尝试将 get/set 添加到 Element.prototype 时,它们的语法如何? MDN 上的示例显示了一个局部变量,但是如何使用它们添加到 Element.prototype 中?
Object.defineProperty {Element.prototype, "dataset",
get: function() { /* return value */ }
, set: function(newVal) { /* set somehow w/ setAttribute or jQuery */ }
}
我想将 getter/setter 方法路由到 $.attr() 上面的示例只是将它们与 .data() 或(更好)本机 setAttribute
相关联和 getAttribute。 这里有一个用于数据集的polyfill,但它只支持符合标准的浏览器(不是 IE8 或更低版本)。我想做一个避免使用 __defineGetter__
(我认为这是 IE8 中的问题)。我认为 defineProperty 可能是合适的方法,我可以使用ES5 Shim 来填充它。我将如何使用 defineProperty 来执行此操作?
I'm trying to figure out how to make a polyfill for dataset and I think I need to use get and set to define the function. How would the syntax of get/set be when trying to add them to Element.prototype
? The examples on MDN shows a local variable, but how do you use them to add to Element.prototype
?
Object.defineProperty {Element.prototype, "dataset",
get: function() { /* return value */ }
, set: function(newVal) { /* set somehow w/ setAttribute or jQuery */ }
}
I'd like to either route the getter/setter methods to $.attr() the example above I just related them to .data() or (better) the native setAttribute
and getAttribute
.
There is one polyfill for dataset here but it only supports standards-compliant browsers (not IE8 or less). I want to do one the avoids the use of __defineGetter__
(I think that's the issue in IE8). I think defineProperty may be the appropriate method, and I can use the ES5 Shim to polyfill that. How would I use defineProperty to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,
Object.defineProperty
在可用的情况下可以工作。我在 Eli Grey 的基础上制作了这样一个填充,通过以下方式进行了一点增强避免全局/非标准原型覆盖并与 CamelCasing 正常工作(并在缺乏defineProperty
支持的情况下利用他的 Xccessors polyfill)。更新:虽然在这种情况下对您来说并不重要,但我可能会提到
defineProperty
polyfill 仅适用于 IE8 中的 DOM 对象。而且它在 IE8 之前不起作用,因为在那之前人们无法覆盖 DOM 原型——除非你真的想亲自动手构建你的自己的,但彻底做到这一点将是一项艰巨的任务。此外,数据集
polyfill(我的和原始的)目前仅以polyfill、标准友好的方式工作,以从Element.prototype.getAttribute( )
并从那里开始工作。Element.prototype.getAttribute
还需要填充以确保它获得最新的数据集更新版本等。Yes,
Object.defineProperty
works where available. I've made such a polyfill based on Eli Grey's original, enhancing a little bit by avoiding globals/non-standard prototype overriding and to work properly with CamelCasing (and utilizing his Xccessors polyfill wheredefineProperty
support is lacking).Update: Although it doesn't matter for you in this case, I might mention that the
defineProperty
polyfill will only work in IE8 on DOM objects. And it will not work before IE8 because one cannot override the DOM prototypes before then--unless you really want to get your hands dirty and build your own, but doing it thoroughly would be a vast task. Also, thedataset
polyfills (mine and the original) currently only work in a polyfill, standard-friendly manner to read in properties fromElement.prototype.getAttribute()
and work from there.Element.prototype.getAttribute
would also need polyfilling in order to ensure it was getting the latest dataset-updated version, etc.