youngwind / blog

梁少峰的个人博客
4.66k stars 385 forks source link

es5-shim和es5-sham #34

Open youngwind opened 8 years ago

youngwind commented 8 years ago

起因

在做项目的时候发现ie8不兼容Array.prototype.forEach方法,找到了两个解决方案,jquery.each和es5-shim

解决方案

1. jquery.each

$.each(array, function(key, value){
   // array[key] === value;
});

2. es5-shim

第二种解决方案是检测是否有这个方法,如果没有,自定义它,es5-shim帮我们做好了这个事情。

安装

bower install es5-shim --save

使用

<script src="./es5-shim.js"></script>
<script src="./es5-sham.js"></script>
// load your script

参考资料: http://stackoverflow.com/questions/412447/for-each-javascript-support-in-ie

ian4hu commented 7 years ago

es5-shim 和 es5-sham 有什么区别 必须要一起用么?