Working around IE8's broken Object.defineProperty implementation

后端 未结 5 1206
一向
一向 2020-12-05 03:14

Consider the following code, using ECMAScript5\'s Object.defineProperty feature:

var sayHi = function(){ alert(\'hi\'); };
var defineProperty =          


        
5条回答
  •  情书的邮戳
    2020-12-05 03:28

    I don't think there's a better way than a direct feature test with try/catch. This is actually exactly what IE team itself recommends in this recent post on transitioning to ES5 API.

    You can shorten the test to just something like Object.defineProperty({}, 'x', {}) (instead of using Array.prototype) but that's a minor quibble; your example tests exact functionality (and so has less chance of false positives).

提交回复
热议问题