What is the use of the JavaScript 'bind' method?

后端 未结 19 1939
自闭症患者
自闭症患者 2020-11-21 06:24

What is the use of bind() in JavaScript?

19条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 06:57

    Another usage is that you can pass binded function as an argument to another function which is operating under another execution context.

    var name = "sample";
    function sample(){
      console.log(this.name);
    }
    var cb = sample.bind(this);
    
    function somefunction(cb){
      //other code
      cb();
    }
    somefunction.call({}, cb);
    

提交回复
热议问题