Working around IE8's broken Object.defineProperty implementation

匿名 (未验证) 提交于 2019-12-03 01:52:01

问题:

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

var sayHi = function(){ alert('hi'); }; var defineProperty = (typeof Object.defineProperty == 'function'); if (defineProperty) Object.defineProperty(Array.prototype,'sayHi',{value:sayHi}); else Array.prototype.sayHi = sayHi; var a = []; a.sayHi(); 

This works for Chrome and Firefox 4 (where defineProperty exists), and it works for Firefox 3.6 (where defineProperty does not exist). IE8, however, only partially supports defineProperty. As a result, it attempts to run the Object.defineProperty method, but then fails (with no error shown in the browser) and ceases to run all other JavaScript code on the page.

Is there a better way to detect and avoid IE8's broken implementation than:

if (defineProperty){   try{ Object.defineProperty(Array.prototype,'sayHi',{value:sayHi}); }catch(e){}; } if (!Array.prototype.sayHi) Array.prototype.sayHi = sayHi; 

For the curious, I'm using this in my ArraySetMath library to define non-enumerable array methods in those browsers that support this, with a fallback to enumerable methods for older browsers.

回答1:

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).



回答2:

I'm using Browserify with the package pluralize from npm which uses Object.defineProperty and I dropped this in.

https://github.com/inexorabletash/polyfill/blob/master/es5.js



回答3:

I stumbled on this before. IMHO using a try…catch statement is too drastic.
Something more efficient would be to use conditional compilation:

/*@cc_on@if(@_jscript_version>5.8)if(document.documentMode>8)@*/ Object.defineProperty && Object.defineProperty(Array.prototype,'sayHi',{value:sayHi}); /*@end@*/  


回答4:

I had the same kind issue (i.e. the Object.defineProperty in IE 8 being DOM only and not a full implementation as the other browsers), but it was for a polyfill..

Anyhoo, I ended using a 'feature' check to see if I was using IE, its not perfect, but it works on all the tests I could do:

if (Object.defineProperty && !document.all && document.addEventListener) {     Object.defineProperty(Array.prototype,'sayHi',{value:sayHi}); } else {     Array.prototype.sayHi = sayHi; } 

as IE document.addEventListener, and document.all is a proprietary Microsoft extension to the W3C standard. These two checks are equivalent to checking if IE is version 8 or below.



回答5:

Array.prototype.sayHi = function(){ alert('hi'); };  try {   Object.defineProperty(Array.prototype, 'sayHi', {     value: Array.prototype.sayHi   }); } catch(e){}; 


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