youngwind / blog

梁少峰的个人博客

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

es5-shim和es5-sham

youngwind opened this issue · comments

起因

在做项目的时候发现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

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