The following content will be divided into the following subsections:
Source of /apply/bind method
()
()
3.1: Find out the maximum number in the array
3.2: Change the empty element of the array to undefined
3.3: Convert array-like objects
()
5. Bind the object of the callback function
, apply, bind method connection and difference
Source of /apply/bind method
First of all, when using call, apply, and bind methods, it is necessary for us to know where these three methods come from? Why can these three methods be used?
The three methods of call, apply, and bind are actually inherited from China and belong to instance methods.
(('call')) //true (('apply')) //true (('bind')) //true
In the above code, all return true, indicating that all three methods are inherited from. Of course, ordinary objects, functions, and arrays all inherit three methods in the object, so these three methods can be used in objects, arrays, and functions.
The concept of inheritance will be shared with you in the future.
()
The call method of a function instance can specify the pointer of this inside the function (that is, the scope where the function is executed), and then call the function in the specified scope. And the function will be executed immediately.
Take an example to understand this passage carefully.
var keith = { rascal: 123 }; var rascal = 456; function a() { (); } a(); //456 (); //456 (null); //456 (undefined); //456 (this); //456 (keith); //123
In the above code, if the this keyword in the a function points to a global object, the return result is 456. As you can see, if the call method has no parameters, or the parameters are null or undefined or this, it is equivalent to pointing to a global object. If you use the call method to point this keyword to the keith object, that is, the scope of the function when it is executed is a keith object, the return result is 123.
The call() method can pass two parameters. The first parameter specifies the pointing of this inside the function (that is, the scope in which the function is executed), and the second parameter is the parameter that needs to be passed when the function is called.
function keith(a, b) { (a + b); } (null, 1, 2); //3
The first parameter is required, it can be null, undefined, this, but cannot be empty. Set to null, undefined, this indicates that the function keith is in global scope at this time. The second parameter must be added one by one. And in apply, it must be added as an array.
One application of the call method is to call the native method of the object. It can also be used to convert class array objects into arrays.
var obj = {}; (('toString')); //false = function() { return true; } (('toString')); //true ((obj, 'toString')); //false
In the above code, hasOwnProperty is a method inherited by the obj object. If this method is overwritten, it will not get the correct result. The call method can solve this method, which puts the original definition of the hasOwnProperty method on the obj object to execute, so that no matter whether there is a method with the same name on obj, it will not affect the result. It should be noted that hasOwnProperty is a method of the native object, and call is a method inherited from.
()
The function of the apply method is similar to the call method, which also changes this point (the scope in which the function is executed), and then calls the function in the specified scope. The function will also be executed immediately. The only difference is that it receives an array as an argument when the function is executed.
The first parameter of the apply method is also the object that this wants to point to. If set to null or undefined or this, it is equivalent to specifying a global object. The second parameter is an array, and all members of the array are used as parameters in turn, and the original function is passed in when called. The parameters of the original function must be added one by one in the call method, but in the apply method, they must be added as an array.
Take a look at the nuances of call, apply.
function keith(a, b) { (a + b); } (null, 2, 3); //5 (null, [2, 3]); //5
In the above code, the first parameter is null, pointing to the global scope; the second parameter is passed in slightly different form.
The apply method has the following applications.
3.1: Find out the maximum number in the array
var a = [2, 4, 5, 7, 8, 10]; ((null, a)); //10 ((null,2, 4, 5, 7, 8, 10)); //10
Javascript does not provide a method to find the maximum value in an array. Using the inherited apply and methods can return the maximum value of the array.
3.2: Change the empty element of the array to undefined
Through the apply method, use the Array constructor to turn the empty elements of the array into undefined.
((null, [1, , 3])); // [1, undefined, 3]
The difference between an empty element and an undefined is that the forEach method of the array will skip the empty element, but will not skip undefined and null. Therefore, when traversing internal elements, you will get different results.
var a = [1, , 3]; (function(index) { (index); //1,3 , skipped empty element. }) (null,a).forEach(function(index){ (index); /////1,undefined,3 , set the empty element to undefined })
3.3: Convert array-like objects
In addition, using the slice method of array objects, an array-like object (such as arguments object) can be converted into a real array. Of course, an important application of the slice method is to convert array-like objects into real arrays. Both call and apply can implement the application.
(({0:1,length:1})); //[1] (({0:1,length:1})); //[1] (({0:1,length:2})); //[1,undefined] (({0:1,length:2})); //[1,undefined] function keith(a,b,c){ return arguments; } ((keith(2,3,4))); //[2,3,4]
The parameters of the call and apply method in the above code are all objects, but the returns are all arrays, which serves the purpose of converting the object into an array. As can be seen from the above code, the premise for this method to work isThe object being processed must have a length attribute and the corresponding numeric keys.
()
The bind method is used to specify this pointer inside the function (the scope in which it is executed) and then return a new function. The bind method does not execute a function immediately.
var keith = { a: 1, count: function() { (++); } }; (); //1 (); //2 (); //3
In the above code, if it points to the a property inside the keith object, if this method is assigned to another variable, an error will occur when calling it.
var keith = { a: 1, count: function() { (++); } }; var f = ; f(); //NaN
In the above code, if the count method is assigned to the f variable, then this object points to no longer a keith object, but a window object. The default is undefined. After performing incremental operations, undefined++ is equal to NaN.
To solve this problem, you can use the bind method to bind this in the keith object to the keith object, or call it directly.
var f = (keith); f(); //1 f(); //2 f(); //3 (keith)() //1 (keith)() //2 (keith)() //3
Of course, this can also be bound to other objects.
var obj = { a: 100 }; var f = (obj); f(); //100 f(); //101 f(); //102
Similarly, we can also pass parameters to the bind method. If the first parameter is null or undefined or this, it will point this object inside the function to the global environment; the second is the parameter required when calling, and the form of the parameter is passed is the same as the call method.
function keith(a, b) { return a + b; } ((null,[1,4])); //5 ((null,1,4)); //5 ((null, 1, 4)); //keith() ((null, 1, 4)()); //5
In the above code, you can see the difference between call, apply and bind:Both call and apply methods are executed immediately after the call is called. After the bind call, it returns to the original function, and it needs to be called again, a bit like a closure. If you are not familiar with the concept of closure, you can browse these two articles:Deeply understand javascript function parameters and closures,A brief discussion on JavaScript's closure function。
5. Bind the object of the callback function
In this articleDetailed explanation of this keyword in javascriptIn this, it is mentioned that if this object is used in the retrieval function, then this object will point to the DOM object, that is, the button object. If you want to solve this pointing problem in the callback function, you can use the following method.
var o = { f: function() { (this === o); } } $('#button').on('click', function() { (o); //or (o); //or (o)(); });
After clicking the button, the console will display true. Since the apply method (or call method) not only binds the object where the function is executed, but also executes the function immediately (while the bind method will not execute immediately, pay attention to the difference), the binding statement has to be written in a function body.
, apply, bind method connection and difference
In fact, it is used to specify the problem of this pointing inside the function. These three methods are similar, but there are formal differences. Readers can use three methods to try to implement the above examples.
To summarize the call, apply, bind methods:
a: The first parameter is the pointer of this in the specified function (the scope where the function is executed), and then the function is called according to the specified scope.
b: All can pass parameters when function calls. call, bind methods need to be passed in directly, while apply methods need to be passed in as arrays.
c: call, apply method executes the function immediately after the call, while bind method is not executed immediately, and the function needs to be executed again. It smells like a closure.
d: The problem of changing the pointing of this object is not only the call, apply, and bind methods, but also the that variable can be used to fix this pointing. If you have any questions, please visitDetailed explanation of this keyword in javascript
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!