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

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

What is the use of bind() in JavaScript?

19条回答
  •  天命终不由人
    2020-11-21 06:45

    bind is a function which is available in java script prototype, as the name suggest bind is used to bind your function call to the context whichever you are dealing with for eg:

        var rateOfInterest='4%';
        var axisBank=
        {
        rateOfInterest:'10%',
        getRateOfInterest:function()
        {
        return this.rateOfInterest;
        }
        }
        axisBank.getRateOfInterest() //'10%' 
    
    
        let knowAxisBankInterest=axisBank.getRateOfInterest // when you want to assign the function call to a varaible we use this syntax
        knowAxisBankInterest(); // you will get output as '4%' here by default the function is called wrt global context
    
    let knowExactAxisBankInterest=knowAxisBankInterest.bind(axisBank);     //so here we need bind function call  to its local context
    
    
        knowExactAxisBankInterest() // '10%' 

提交回复
热议问题