call实现

call实现

Function.prototype.call = function(context) {
    if (typeof this !== 'function') {
        throw new TypeError('not a func')
    }
    context = context || {}
    context.fn = this
    var args = []
    for (var i = 1; i < arguments.length; i++) {
        args.push(arguments[i])
    }
    var result = eval('context.fn(' + args +')')
    delete context.fn
    return result
}

apply实现

Function.prototype.apply = function(context) {
    if (typeof this !== 'function') {
        throw new TypeError('not a func')
    }
    context = context || {}
    context.fn = this
    var args = arguments[1]
    var result
    if (args) {
        result = eval('context.fn(' + args + ')')
    } else {
        result = context.fn()
    }
    delete context.fn
    return result
}

bind实现

filter实现

instanceof实现

new实现

Last updated