手写apply、call、bind

apply

作用:改变this执行,函数立即执行,参数以数组传递

思路:

        1、在this新指向的对象上,增加一个函数等于待执行函数

        2、去参数

        3、执行函数

        4、删除增加的函数,返回结果

    // apply
    function applyTest() {
        var person = {
            fullName: function (city, country) {
                return this.firstName + " " + this.lastName + "," + city + "," + country;
            }
        }
        var person1 = {
            firstName: "Bill",
            lastName: "Gates"
        }
        console.log(person.fullName.myApply(person1, ["Oslo", "Norway"]));
    }

    // 手写applyTest (参数以数组形式在第二个参数传递, 立即执行)
    Function.prototype.myApply = function (context) {
        if (typeof this !== 'function') {
            conole.error('this is not a function');
        }
        let result = null;
        context = context || window;
        context.fn = this; // 将函数绑定到当前上下文中
        result = context.fn(...arguments[1]); //取得第二个参数(数组)并展开传给函数 
        delete context.fn; // 删除新增的函数
        return result;
    }

call

作用:改变this执行,函数立即执行,参数依次传递

思路:

        1、在this新指向的对象上,增加一个函数等于待执行函数

        2、取参数

        3、执行函数

        4、删除增加的函数,返回结果

    // call
    function callTest() {
        var person = {
            fullName: function (city, country) {
                return this.firstName + " " + this.lastName + "," + city + "," + country;
            }
        }
        var person1 = {
            firstName: "Bill",
            lastName: "Gates"
        }
        console.log(person.fullName.myCall(person1, "Oslo", "Norway"));
    }

    // 手写call (参数依次展开, 立即执行)
    Function.prototype.myCall = function(context) {
        if (typeof this !== 'function') {
            console.error('this is not a function');
        }
        context = context || window;
        let result = null;
        context.fn = this; // 将函数绑定到当前上下文中
        result = context.fn(...[...arguments].slice(1)); //取得第除第一个参数之外的参数并展开传给函数 
        delete context.fn;  // 删除新增的函数
        return result;
    }

bind

作用:改变this执行,返回一个函数,参数依次传递

思路:

        1、指定一个函数等于当前函数

        2、返回一个新函数

        3、函数中使用apply立即执行函数

    function bindTest() {
        var person = {
            fullName: function (city, country) {
                return this.firstName + " " + this.lastName + "," + city + "," + country;
            }
        }
        var person1 = {
            firstName: "Bill",
            lastName: "Gates"
        }
        console.log(person.fullName.myBind(person1, "Oslo", "Norway")());
    }    
    // 手写bind (参数依次展开,不立即执行)
    Function.prototype.myBind = function(context) {
        if (typeof this !== 'function') {
            console.error('this is not a function');
        }
        let args = [...arguments].slice(1),
        fn = this;

        return function Fn() {
            return fn.apply(
                this instanceof Fn ? this : context, // 因为返回的函数,防止new执行,所以判断一下,如果是new出来的执行this环境下的,如果不是就是新上下文环境下的
                args.concat(...arguments)
            )
        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值