In JavaScript, bind, apply, and call methods can all explicitly bind the context this, what is the difference between these three?
Bind bind only this does not execute immediately
varperson ={firstname:' Darren ', LastName:' Ji ', Getfullname:function(){ varFullName = This. FirstName + "+ This. LastName; returnfullname; }};//in the browser, this is pointing to windowvarLogName =function(Arg1, arg2) {Console.log ( This. Getfullname ());}//bind just sets the point of this and does not itself performvarLogpersonname =Logname.bind (person);//Darren JiLogpersonname ();
Call not only binds this, but also executes immediately and accepts variable parameters
varperson ={firstname:' Darren ', LastName:' Ji ', Getfullname:function(){ varFullName = This. FirstName + "+ This. LastName; returnfullname; }};//in the browser, this is pointing to windowvarLogName =function(Arg1, arg2) {Console.log ( This. Getfullname ()); Console.log (' Arguments: ' + arg1 + ' +arg2);}//Darren Ji//arguments:en esLogname.call (person, ' en ', ' es ');
Apply not only binds this, but also executes immediately, and accepts the array
varperson ={firstname:' Darren ', LastName:' Ji ', Getfullname:function(){ varFullName = This. FirstName + "+ This. LastName; returnfullname; }};//in the browser, this is pointing to windowvarLogName =function(Arg1, arg2) {Console.log ( This. Getfullname ()); Console.log (' Arguments: ' + arg1 + ' +arg2);}//Darren Ji//arguments:en esLogname.apply (person, [' en ', ' es ']);
The difference between Bind,apply,call