标签:style blog io java ar for 2014 div sp
ECMAScript5新增的数组方法看似都比较简单其实还是有一些细节需要了解的var a = new Array(10); a[0] = null ; a[1] = undefined; var _a = a.map(function(){ return 1; }); console.info(a);//[ null, undefined, , , , , , , , , ] console.info(_a);//[ 1, 1, , , , , , , , , ] _a = a.filter(function(){ return true; }); console.info(_a);//[ null, undefined ]
var each = Array.prototype.forEach ? Array.prototype.forEach : function(callback) { var arr=this; for (var i in arr) { var item = arr[i]; callback.call(item, item, i, arr); } } Array.prototype.each = each; var map = Array.prototype.map ? Array.prototype.map : function(callback) { var arr = this; var _arr = arr.slice(0); for (var i in arr) { var item = arr[i] _arr[i] = callback(item, item); } return _arr; } Array.prototype.map = map; var filter= Array.prototype.filter ? Array.prototype.filter : function(callback) { var r = []; var arr = this; for (var i in arr) { var item = arr[i]; if(callback.call(item, item)) { r.push(item); } } return r; } Array.prototype.filter = filter;
标签:style blog io java ar for 2014 div sp
原文地址:http://blog.csdn.net/songzheng_741/article/details/39459239