ECMAScript6新特征的Array API

一致 填充数组

var arr = new Array(5);
arr.fill('abc',2,4);
console.log('Array.prototype.fill',arr); // [undefined × 2, "abc", "abc", undefined × 1]

 

二 映射到新数组

// 映射稀疏数组
var arr = new Array(5);
arr[0] = 1;
arr[2] = 3;
arr[4] = 5;
var newArr = arr.map(function(x){
    return Math.pow(x,2);
});
console.log('Array.prototype.map',newArr);  // [1, undefined × 1, 9, undefined × 1, 25]
// 映射类数组对象
var obj = {
    '0' : 2,
    '2' : 4,
    '4' : 6,
    length : 5
};
var ObjArr = Array.prototype.map.call(obj,function(x){
    return Math.pow(x,2);
});
console.log('Array.prototype.map',ObjArr); // [4, undefined × 1, 16, undefined × 1, 36]