JavaScript中数组高级编程实践-2

我们来 看 EcmaScript5 规范中的 数组新的API ,它们是非常有用的,

介绍完这一部分 ,我们将用 Array 数组 这个对象 来构建 一个类似于Java中ArrayList 类,

以便于封装 通用 的逻辑,实现代码复用。


API :

/**
@param {Function} callback
@param {Object} [initialValue]
@return {Object}
*/
Array.prototype.reduce = function(callback,initialValue) {};
/**
@param {Function} callback
@param {Object} [initialValue]
@return {Object}
*/
Array.prototype.reduceRight = function(callback,initialValue) {};
/**
@param {Object} searchElement
@param {number} [fromIndex]
@return {number}
*/
Array.prototype.indexOf = function(searchElement,fromIndex) {};
/**
@param {Object} searchElement
@param {number} [fromIndex]
@return {number}
*/
Array.prototype.lastIndexOf = function(searchElement,fromIndex) {};
/**
@param {Function} callback
@param {Object} [thisObject]
@return {boolean}
*/
Array.prototype.every = function(callback,thisObject) {};
/**
@param {Function} callback
@param {Object} [thisObject]
@return {Array}
*/
Array.prototype.filter = function(callback,thisObject) {};
/**
@param {Function} callback
@param {Object} [thisObject]
@return {void}
*/
Array.prototype.forEach = function(callback,thisObject) {};
/**
@param {Function} callback
@param {Object} [thisObject]
@return {Array}
*/
Array.prototype.map = function(callback,thisObject) {};
/**
@param {Function} callback
@param {Object} [thisObject]
@return {boolean}
*/
Array.prototype.some = function(callback,thisObject) {};
/**
@param {Object} object
@return {boolean}
*/
Array.prototype.isArray = function(object) {};

使用:

/**
 *@class MyEcmaScript5
 *@description
 *@time 2014-09-16 21:38
 *@author StarZou
 **/

// 定义一个数组,存储不同数据类型的元素
var array = [8, 2, 1, 5],
    array2 = [
        [0, 1],
        [1, 2],
        [2, 3]
    ],
    value;

/// Array.prototype.reduce = function(callback,initialValue) {};
// 化解数组:
// 调用reduce 方法提供的回调函数,
// 总共调用 数组的lenght - 1次 回调函数
// 第一次时 previousValue 为 initialValue
// 此后 reduce方法的返回值 作为 previousValue

// reduceRight 则从数组最右边开始,进行上述操作


// 数组中元素累加
value = array.reduce(function (previousValue, currentValue, index, array) {
        return previousValue + currentValue;
    }
);
console.log(value);  // 16

// 数组扁平化
value = array2.reduce(function (previousValue, currentValue, index, array) {
        return previousValue.concat(currentValue);
    }
);
console.log(value); // [ 0, 1, 1, 2, 2, 3 ]


/// Array.prototype.indexOf = function(searchElement,fromIndex) {};
// 从fromIndex索引处 向后寻找searchElement元素 找到并返回其索引,否则返回-1
// fromIndex 默认为 0
console.log(array.indexOf(1)); // 2
console.log(array.indexOf(3)); // -1


/// Array.prototype.lastIndexOf = function(searchElement,fromIndex) {};
// 从fromIndex索引处 向前寻找searchElement元素 找到并返回其索引,否则返回-1
console.log(array.lastIndexOf(1)); // 2
console.log(array.lastIndexOf(3)); // -1


/// Array.prototype.every = function(callback,thisObject) {};
// 测试数组的所有元素是否都通过了指定函数的测试
value = array.every(function (element, index, array) {
    return element > 1;
});
console.log(value); // false


/// Array.prototype.filter = function(callback,thisObject) {};
// 数组过滤:
// 返回通过函数测试的 元素(当回调函数的返回true,表明元素通过测试 ),生成新的数组
value = array.filter(function (element, index, array) {
    return element > 1;
});
console.log(value);// [ 8, 2, 5 ]


/// Array.prototype.forEach = function(callback,thisObject) {};
// 为每个数组元素执行一次回调函数。
array.forEach(function (element, index, array) {
    console.log(element);
});


/// Array.prototype.map = function(callback,thisObject) {};
// 数组映射:
// 返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。
value = array.map(function (element, index, array) {
    return element * element;
});
console.log(value); // [ 64, 4, 1, 25 ]


/// Array.prototype.some = function(callback,thisObject) {};
// 测试数组中的某些元素是否通过了指定函数的测试。
value = array.some(function (element, index, array) {
    return element > 1;
});
console.log(value); // true


/// Array.prototype.isArray = function(object) {};
// 判断元素是否为数组类型
console.log(Array.isArray(array)); // true

运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值