The difference between js display binding call(), apply() and bind()

note: 

In the case of call(), the parameters need to be passed one by one, so when we only pass a value parameter or no value parameter, please use call(). If you want to pass multiple value parameters, use apply(). 

getName.bind() will not call the function getName(). It returns a new function newFn, which we can call like newFn().

 var getName = function(hobby1, hobby2) {
	  console.log(this.name + ' likes ' + hobby1 + ' , ' + hobby2);
  }
  var user = {
	name: 'Tapas',
	address: 'Bangalore'  
  };
 // call()方法
  var hobbies = ['Swimming', 'Blogging'];
  getName.call(user, hobbies[0], hobbies[1]);
 // apply()方法
  var hobbies = ['Swimming', 'Blogging'];
  getName.apply(user, hobbies);
 // apply()方法
  var hobbies = ['Swimming', 'Blogging'];
  var newFn = getName.bind(user, hobbies[0], hobbies[1]); 
   newFn();

 

Guess you like

Origin blog.csdn.net/baidu_39043816/article/details/108535890