为了账号安全,请及时绑定邮箱和手机立即绑定

热点面试题:JS 中 call, apply, bind 概念、用法、区别及实现?

前言

极度投入,深度沉浸,边界清晰

前端小菜鸡一枚,分享的文章纯属个人见解,若有不正确或可待讨论点可随意评论,与各位同学一起学习~

热点面试题:JS 中 call, apply, bind 概念、用法、区别及实现?

概念:

  • function.call(thisArg, arg1, arg2, ...)
  • function.apply(thisArg, [arg1, arg2, ...])
  • function.bind(thisArg, arg1, arg2, ...)
  • 三者都是改变 this 指向,通过一个参数或多个参数来调用一个函数的。

用法:

let obj = {
    name: "哈哈",
    sayName: function () {
        console.log("sayName", this.name);
        return this.name;
    },
    eat: function (food1, food2) {
        console.log("eat", food1, food2);
    },
};

let obj2 = {
    name: "是的",
};

obj.sayName.call(obj2); // sayName 是的
obj.eat.call(obj2, "鱼", "肉"); // eat 鱼 肉

obj.eat.apply(obj2, ["鱼", "肉"]); // e at 鱼 肉

obj.eat.bind(obj2, "鱼", "肉"); // 不会调用,需要一个结果来接收
let res = obj.eat.bind(obj2, "鱼", "肉");
res(); // eat 鱼 肉

区别:

  • call 与 bind 的区别?
    • call 会直接调用,而 bind 会创建一个新的函数作为一个返回值进行调用, 而其余参数将作为新函数的参数,供调用时使用
  • call 与 apply 的区别?
    • 主要区别在第二个参数中,call 接受的是一个参数列表,也就是一个个参数,而 apply 接受的是一个包含多个参数的数组

实现:

  • function.call(thisArg, arg1, arg2, ...)
Function.prototype.myCall = function (context, ...args) {
    // 条件判断,判断当前调用的对象是否为函数,
    if (Object.prototype.toString.call(this).slice(8, -1) != "Function")
        throw new Error("type error");
    // 判断传入上下文对象是否存在,如果不存在,则设置为 window
    if (!context || context === null) context = window;
    // 创建唯一的 key 值,作为构建的 context 内部方法名
    let fn = Symbol();
    // 将 this 指向调用的 call 函数
    context[fn] = this;
    // 执行函数并返回结果 === 把自身作为传入的 context 的方法进行调用
    return context[fn](...args);
};
let obj = {
    name: "哈哈",
    sayName: function () {
        console.log("sayName", this.name);
        return this.name;
    },
    eat: function (food1, food2) {
        console.log("eat", food1, food2);
    },
};
let obj2 = {
    name: "是的",
};
obj.sayName.myCall(obj2);
  • function.apply(thisArg, [arg1, arg2, ...])
Function.prototype.MyApply = function (context, args) {
    // 条件判断,判断当前调用的对象是否为函数,
    if (Object.prototype.toString.call(this).slice(8, -1) != "Function")
        throw new Error("type error");
    // 判断传入上下文对象是否存在,如果不存在,则设置为 window
    if (!context || context === null) context = window;
    // 创建唯一的 key 值,作为构建的 context 内部方法名
    let fn = Symbol();
    // 将 this 指向调用的 call 函数
    context[fn] = this;
    // 执行函数并返回结果 === 把自身作为传入的 context 的方法进行调用
    return context[fn](...args);
};
let obj = {
    name: "哈哈",
    sayName: function () {
        console.log("sayName", this.name);
        return this.name;
    },
    eat: function (food1, food2) {
        console.log("eat", food1, food2);
    },
};
let obj2 = {
    name: "是的",
};
obj.sayName.MyApply(obj2, []);
  • function.bind(thisArg, arg1, arg2, ...)
Function.prototype.myBind = function (context, ...args) {
    if (!context || context === null) {
        context = window;
    }
    // 创造唯一的key值  作为我们构造的context内部方法名
    let fn = Symbol();
    context[fn] = this;
    let _this = this;
    //  bind情况要复杂一点
    const result = function (...innerArgs) {
        // 第一种情况: 若是将 bind 绑定之后的函数当作构造函数,通过 new 操作符使用,则不绑定传入的 this,而是将 this 指向实例化出来的对象
        // 此时由于new操作符作用  this指向result实例对象  而result又继承自传入的_this 根据原型链知识可得出以下结论
        // this.__proto__ === result.prototype   //this instanceof result =>true
        // this.__proto__.__proto__ === result.prototype.__proto__ === _this.prototype; //this instanceof _this =>true
        if (this instanceof _this === true) {
            // 此时this指向指向result的实例  这时候不需要改变this指向
            this[fn] = _this;
            this[fn](...[...args, ...innerArgs]); //这里使用es6的方法让bind支持参数合并
        } else {
            // 如果只是作为普通函数调用  那就很简单了 直接改变this指向为传入的context
            context[fn](...[...args, ...innerArgs]);
        }
    };
    // 如果绑定的是构造函数 那么需要继承构造函数原型属性和方法
    // 实现继承的方式: 使用Object.create
    result.prototype = Object.create(this.prototype);
    return result;
};
//用法如下
function Person(name, age) {
    console.log(name); //'我是参数传进来的name'
    console.log(age); //'我是参数传进来的age'
    console.log(this); //构造函数this指向实例对象
}
// 构造函数原型的方法
Person.prototype.say = function () {
    console.log(123);
};
let obj = {
    objName: "我是obj传进来的name",
    objAge: "我是obj传进来的age",
};
// 普通函数
function normalFun(name, age) {
    console.log(name); //'我是参数传进来的name'
    console.log(age); //'我是参数传进来的age'
    console.log(this); //普通函数this指向绑定bind的第一个参数 也就是例子中的obj
    console.log(this.objName); //'我是obj传进来的name'
    console.log(this.objAge); //'我是obj传进来的age'
}
// 先测试作为构造函数调用
let bindFun = Person.myBind(obj, "我是参数传进来的name");
let a = new bindFun("我是参数传进来的age");
a.say(); //123
// 再测试作为普通函数调用
// let bindFun = normalFun.myBind(obj, '我是参数传进来的name')
//  bindFun('我是参数传进来的age')

文章特殊字符描述:

  1. 问题标注 Q:(question)
  2. 答案标注 R:(result)
  3. 注意事项标准:A:(attention matters)
  4. 详情描述标注:D:(detail info)
  5. 总结标注:S:(summary)
  6. 分析标注:Ana:(analysis)
  7. 提示标注:T:(tips)
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消