Object.defineProperty for all browsers?

后端 未结 3 1164
青春惊慌失措
青春惊慌失措 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: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.

提交回复
热议问题