javascript 解决 IE8 损坏的 Object.defineProperty 实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4819693/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Working around IE8's broken Object.defineProperty implementation
提问by Phrogz
Consider the following code, using ECMAScript5's Object.definePropertyfeature:
考虑以下代码,使用 ECMAScript5 的Object.defineProperty特性:
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 definePropertyexists), and it works for Firefox 3.6 (where definePropertydoes not exist). IE8, however, only partially supports defineProperty. As a result, it attempts to run the Object.definePropertymethod, but then fails (with no error shown in the browser) and ceases to run all other JavaScript code on the page.
这适用于 Chrome 和 Firefox 4(defineProperty存在的地方),也适用于 Firefox 3.6(defineProperty不存在的地方)。但是,IE8仅部分支持defineProperty. 结果,它尝试运行该Object.defineProperty方法,但随后失败(浏览器中未显示错误)并停止运行页面上的所有其他 JavaScript 代码。
Is there a better way to detect and avoid IE8's broken implementation than:
有没有比以下更好的方法来检测和避免 IE8 的破坏实现:
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 ArraySetMathlibrary to define non-enumerable array methods in those browsers that support this, with a fallback to enumerable methods for older browsers.
出于好奇,我在我的ArraySetMath库中使用它在支持它的浏览器中定义不可枚举的数组方法,并回退到旧浏览器的可枚举方法。
采纳答案by kangax
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.
我认为没有比使用 try/catch 进行直接功能测试更好的方法了。这实际上正是 IE 团队本身在最近关于过渡到 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).
您可以将测试缩短为类似Object.defineProperty({}, 'x', {})(而不是使用Array.prototype),但这是一个小问题;您的示例测试了确切的功能(因此误报的可能性较小)。
回答by Michael Benin
I'm using Browserify with the package pluralize from npm which uses Object.defineProperty and I dropped this in.
我正在使用 Browserify 和来自 npm 的包复数,它使用 Object.defineProperty,我把它放进去。
https://github.com/inexorabletash/polyfill/blob/master/es5.js
https://github.com/inexorabletash/polyfill/blob/master/es5.js
回答by Knu
I stumbled on this before. IMHO using a try…catch statement is too drastic.
Something more efficient would be to use conditional compilation:
我之前偶然发现了这个。恕我直言,使用 try...catch 语句太激烈了。
更有效的方法是使用条件编译:
/*@cc_on@if(@_jscript_version>5.8)if(document.documentMode>8)@*/
Object.defineProperty && Object.defineProperty(Array.prototype,'sayHi',{value:sayHi});
/*@end@*/ 
回答by Dev Ops
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..
我有同样的问题(即 IE 8 中的 Object.defineProperty 只是 DOM,而不是其他浏览器的完整实现),但它是针对 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:
Anyhoo,我结束了使用“功能”检查来查看我是否使用 IE,它并不完美,但它适用于我可以做的所有测试:
if (Object.defineProperty && !document.all && document.addEventListener) {
    Object.defineProperty(Array.prototype,'sayHi',{value:sayHi});
} else {
    Array.prototype.sayHi = sayHi;
}
as IE <= 8 has no document.addEventListener, and document.allis a proprietary Microsoft extension to the W3C standard. These two checks are equivalent to checking if IE is version 8 or below.
因为 IE <= 8 没有document.addEventListener,并且document.all是 W3C 标准的专有 Microsoft 扩展。这两个检查相当于检查 IE 是否为 8 或以下版本。
回答by Abdullah Ayd?n
Array.prototype.sayHi = function(){ alert('hi'); };
try {
  Object.defineProperty(Array.prototype, 'sayHi', {
    value: Array.prototype.sayHi
  });
}
catch(e){};

