首页 > 其他分享 >改变 this 指向的方法 call()、apply()、 bind()的区别 ?

改变 this 指向的方法 call()、apply()、 bind()的区别 ?

时间:2022-09-02 09:57:02浏览次数:85  
标签:name bind dog cat call apply

call()第一个参数是this的新指向;第二个参数是要传入函数的参数

    let cat = {
      name: "喵喵"
    }
    let dog = {
      name: "旺财",
      eat(food) {
        console.log(this.name, "喜欢吃", food)
      },
      eatMore(food1, food2) {
        console.log(this.name, "喜欢吃", food1, food2)
      }
    }
    dog.eat("骨头"); //旺财喜欢吃骨头
    dog.eat.call(cat); // 喵喵 喜欢吃 undefined
    dog.eat.call(cat, "鱼"); // 喵喵 喜欢吃 鱼
    dog.eatMore.call(cat, "鱼", "肉"); //喵喵 喜欢吃 鱼 肉

apply和call的区别是传参不同,apply的参数通过数组形式传递

    let cat = {
      name: "喵喵"
    }
    let dog = {
      name: "旺财",
      eat(food) {
        console.log(this.name, "喜欢吃", food)
      },
      eatMore(food1, food2) {
        console.log(this.name, "喜欢吃", food1, food2)
      }
    }
    dog.eatMore.call(cat, "鱼", "肉"); //喵喵 喜欢吃 鱼 肉
    dog.eatMore.apply(cat, ["鱼", "肉"]);//喵喵 喜欢吃 鱼 肉

bind的传参和call一样

bind和call,apply的区别在于,bind将函数作为返回值(即bind不会立即执行而是在下次调用的时候执行,且bind只在第一次改变this指向有效,后续再次使用不在改变this指向)

var name = "佩奇"
    function fun() {
      console.log(this.name);
    }
    let cat = {
      name: "喵喵"
    }
    let dog = {
      name: "旺财",
    }
    fun()//佩奇
    var f1 = fun.bind(cat);
    f1(); // 喵喵

    // 第二次使用bind绑定无效
    var f2 = f1.bind(dog);
    f2(); // 喵喵

    // 使用call绑定也无效
    f1.call(dog); // 喵喵
    //使用apply无效
    f1.apply(dog); //喵喵

 

标签:name,bind,dog,cat,call,apply
From: https://www.cnblogs.com/qianduan-Wu/p/16648752.html

相关文章

  • 在style中使用v-bind
    在style中使用v-bind最近在公司写项目的时候想实现一个更改主题的功能,查阅网上资料大家给了多种方案,但是其中一种方案比较吸引我,卧槽?什么东西,Vue3可以在style中使用v-bind......
  • 【CV算法基础】FocalLoss理解
     作者提出focalloss的出发点也是希望one-stagedetector可以达到two-stagedetector的准确率,同时不影响原有的速度。既然有了出发点,那么就要找one-stagedetector的准确......
  • 问题积累 - IAR - ErrorTa97:Cannot callintrinsic functionnounwwind _DSBfrom Thumb
    IAR编译工程时报错:ErrorTa97:Cannotcallintrinsicfunctionnounwwind_DSBfromThumbmodeinthisarchitecture详细问题: 问题原因:没有设置芯片类型与型号。解决方......
  • 对C语言中回调函数(callback)的一点理解
    一般函数形式:functiona(char*a,Stringb):接收的参数是一般类型;特殊函数:functionb(char*a,functionc):接收的其中一个参数是函数(只是为了表述方便这么写,实际应该......
  • Cesiun 之 CallbackProperty
    CallbackPropertyCesium号称是集显示时空数据于一体的三维引擎。空间数据的展示我们已经见到,对于时间上的数据,我觉得CallbackProperty是最大功臣。因为使用CallbackProper......
  • uvm callback and reg backdoor access callback
    uvmcallbackandregbackdooraccesscallbackusinguvm_callbackclassLinux上写的,没有中文输入法,也不翻译了,留个档。thereare4mainstepstousinguvm_callback......
  • StatementCallback; bad SQL grammar [DROP TABLE IF EXISTS
    StatementCallback;badSQLgrammar[DROPTABLEIFEXISTS javaspringboot项目自动建表报错 在配置文件中,spring.datasource.url 后追加参数:allowMultiQueri......
  • 彻底弄懂bind,apply,call三者的区别 (转)
    为什么要改变this指向?我们知道bind,call,apply的作用都是用来改变this指向的,那为什么要改变this指向呢?请看下面的例子:varname="lucy";letobj={name:"martin",say:......
  • Fan Noise in Zoom Calls
    FanNoiseinZoomCallsInsomelaptopstheinternalmicrophoneandfanareclosetooneanothercausingthesoundofthefantobeheardbythemicrop......
  • redis 0: "AUTH <password> called without any password configured for the def
    运行项目的时候,报redis0:"AUTH<password>calledwithoutanypasswordconfiguredforthedef原因:主要是redis没有设置密码解决步骤:1.先进入到redis容器中......