The easiest way to use bind() to bind() is to create a function so that the function has the same value no matter how it is called. Unlike call and apply, which simply set the value of this and pass the arguments, it will also bind all the actual arguments passed into the bind() method (the parameters after the first parameter) with this.
For examples of this feature, please see the original text of "JS Authoritative Guide":
var sum = function(x,y) { return x + y }; var succ = sum.bind(null, 1); //Let this point to null, and the subsequent actual parameters will also be passed into the bound function sumsucc(2); // => 3: You can see that 1 is bound to x in the sum functionSecondly, the length of the function returned by the bind() method is equal to the original function's shape parameter amount minus the actual parameter amount passed into the bind() method (all parameters after the first parameter), because the actual parameters passed into bind will be bound to the original function's formal parameters, for example:
function func(a,b,c,d){...} //The length of func is 4var after = func.bind(null,1,2); //The two actual parameters (1,2) are entered here to bind to the a of the func function, bconsole.log(after.length); //The length of after is 2Third, when the function returned by bind() is used as a constructor, this passed into bind() will be ignored, and all actual accounts will be passed into the original function. This is very abstract. For example:
function original(x){ this.a = 1; this.b = function(){return this.a + x}}var obj={ a = 10}var newObj = new(original.bind(obj, 2)); //A real parameter 2console.log(newObj.a); //Output 1, indicating that obj(this value) is ignored when the returned function is used as a constructor console.log(newObj.b()); //Output 3, indicating that the passed parameter 2 is passed into the original function originalThe above is the characteristics of the bind method in ES5, and this technology is also called functional currying . This technique turns a function with multiple parameters into a function with only one parameter. The bind method is the practice of this technology in js.
This brief discussion on the bind method and function currying in JS is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.