Object.defineProperty for all browsers?

若如初见. 提交于 2019-12-02 20:32:29

According to ES5-shim:

/!\ Object.defineProperty

This method will silently fail to set "writable", "enumerable", and "configurable" properties.

Providing a getter or setter with "get" or "set" on a descriptor will silently fail on engines that lack "defineGetter" and "defineSetter", which include all versions of IE up to version 8 so far.

IE 8 provides a version of this method but it only works on DOM objects. Thus, the shim will not get installed and attempts to set "value" properties will fail silently on non-DOM objects.

https://github.com/kriskowal/es5-shim/issues#issue/5

So you know your answer. It can be done on DOM elements, that's it (and on IE8 only).

I'd suggest you just use get/set methods if you want IE7 to work.

For older IEs you'd have to make sure your property is a dom object (even a fake tag) and use onPropertyChange to get notified. See this post by John Dyer for more details.

ryanve

I've had this same question myself. (See here.) It doesn't look like it's fully possible in IE8 or lower. Otherwise the ES5 Shim is your best bet.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!