Object.defineProperty for all browsers?

后端 未结 3 1177
青春惊慌失措
青春惊慌失措 2021-02-02 12:04

Asking about Object.defineProperty as demonstrated below:

function testComponent(){
    var testProperty;
    Object.defineProperty(this, \"testProperty\",
    {         


        
相关标签:
3条回答
  • 2021-02-02 12:45

    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.

    0 讨论(0)
  • 2021-02-02 12:49

    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.

    0 讨论(0)
  • 2021-02-02 12:50

    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.

    0 讨论(0)
提交回复
热议问题