# Array.prototype.filter()

Array.prototype.filter = function (callback, thisArg) {
    // 处理数组类型异常
    if (this === null || this === undefined) {
        throw new TypeError("Cannot read property 'filter' of null or undefined")
    }
    // 处理回调类型异常
    if (Object.prototype.toString.call(callback) !== '[object Function]') {
        throw new TypeError(callback + ' is not a function')
    }

    let O = Object(this),
        len = O.length >>> 0,
        res = [];

    for (let i = 0; i < len; i++) {
        if (i in O) { 
            if (callback.call(thisArg, O[i], i, O)) {
                res.push(O[i]) 
            }
        }
    }

    return res;
} 

参考: