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

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

What is the use of bind() in JavaScript?

19条回答
  •  灰色年华
    2020-11-21 06:57

    Consider the Simple Program listed below,

    //we create object user
    let User = { name: 'Justin' };
    
    //a Hello Function is created to Alert the object User 
    function Hello() {
      alert(this.name);
    }
    
    //since there the value of this is lost we need to bind user to use this keyword
    let user = Hello.bind(User);
    user();
    
    //we create an instance to refer the this keyword (this.name);
    

提交回复
热议问题