手写Function.prototype.bind()函数

源码:

支持new 绑定

//兼容IE6写法
var slice = Array.prototype.slice
function ieBind(ieThis) {
    var args = slice.call(arguments, 1)
    // this就是调用bind的函数
    var fn = this
    function fn2 () {
        var args2 = slice.call(arguments, 0)
        return fn.apply(fn2.prototype.isPrototypeOf(this) ? this : ieThis, args.concat(args2))
    }
    fn2.prototype = fn.prototype
    return fn2
}

//es6写法
function _bind(obj, ...arg) {
    const fn = this;// this就是调用bind的函数
    function fn2 (...arg2) {
        return fn.call(this instanceof fn2 ? this : obj, ...arg, ...arg2)
    }
    fn2.prototype = fn.prototype
    return fn2
}

// module.exports = ieBind;
module.exports = _bind;

if (!Function.prototype.bind) {//polyfill
    Function.prototype.bind = _bind
}

测试代码:

const bind = require('../src/index')

test1('测试bind2是否能用')
test2('测试是否能绑定this成功')
test3('测试this, a, b 绑定成功')
test4('测试this, a绑定成功后,传入b调用成功')
test5('new 的时候也绑定了a, b')
test6('new 的时候也绑定了a, b,并且函数中有 prototype.gogo')


function test1(message) {
    console.log(message)
    Function.prototype.bind2 = bind
    console.assert(Function.prototype.bind2 !== undefined)
}

function test2(message) {
    console.log(message)
    Function.prototype.bind2 = bind
    const f1 = function () {
        return this
    }
    const newF1 = f1.bind2({name: 'he'},1,2)
    console.assert(newF1().name === 'he')
}

function test3(message) {
    console.log(message)
    Function.prototype.bind2 = bind
    const f1 = function (a, b) {
        return [this, a, b]
    }
    const newF1 = f1.bind2({name: 'he'},111,2222)
    console.assert(newF1()[0].name === 'he','this')
    console.assert(newF1()[1] === 111,'a')
    console.assert(newF1()[2] === 2222,'b')
}

function test4(message) {
    console.log(message)
    Function.prototype.bind2 = bind
    const f1 = function (a, b) {
        return [this, a, b]
    }
    const newF1 = f1.bind2({name: 'he'},111)
    console.assert(newF1(2222)[0].name === 'he','this')
    console.assert(newF1(2222)[1] === 111,'a')
    console.assert(newF1(2222)[2] === 2222,'b')
}

function test5(message) {
    console.log(message)
    Function.prototype.bind2 = bind
    const f1 = function (a, b) {
        this.a = a
        this.b = b
    }
    const f2 = f1.bind2(undefined, 111, 222)
    const f3 = new f2()
    console.assert(f3.a === 111,'a')
    console.assert(f3.b === 222,'b')
}

function test6(message) {
    console.log(message)
    Function.prototype.bind2 = bind
    const f1 = function (a, b) {
        this.a = a
        this.b = b
    }
    f1.prototype.gogo = function () {}
    const f2 = f1.bind2(undefined, 111, 222)
    const f3 = new f2()
    console.assert(f3.a === 111,'a')
    console.assert(f3.b === 222,'b')
    console.assert(f1.prototype.isPrototypeOf(f3) ,'new后,原型是否继承')
    console.assert(typeof f3.gogo === 'function')
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值