(context,[argsArray])
Call fun immediately, and point the original this of the fun function to the new context object passed in, implementing the same method to reuse it on different objects.
context: The object passed in, replacing the original this from the fun function;
argsArray: an array or class array object, in which the array parameters will be expanded as separate arguments and passed to the fun function. Pay attention to the order of the parameters.
(context,[arg1],[arg2],[…])
The same applies, but the parameter list is different, and the parameters of the call need to be passed in one by one. If you don't know the number of parameters, use apply.
use:
() //Only receive separate parameters, the following method can be used on the array:
(null, array); //The array array parameters will be expanded into separate parameters and then passed in
(arr1,arr2); //Put an array into another array; if you don’t use apply, the subsequent array parameters will be pushed into it as an element.
(arguments); //Use slice method on the class group object
function isArray(obj){ return (obj) === '[object Array]' ; } //Verify whether it is an array
(context,[arg1],[arg2],[…])
Make the context executed by the fun method never change.
arg1: The parameter list to be passed to the new function
Returns a function for subsequent calls, whose function body is the same as the original function fun, but this of the new function points to the newly passed context object. The new function will have the initial parameter arg1/arg2 specified by the bind method.... The actual parameters when calling the new function in the subsequent call must be arranged behind the existing parameters.
//The original function has 4 parametersvar displayArgs = function (val1, val2, val3, val4) { (val1 + " " + val2 + " " + val3 + " " + val4); } var emptyObject = {}; // When generating a new function, the bind method specifies 2 parameters, and the new function will take these two actual parameters.var displayArgs2 = (emptyObject, 12, "a"); // Pass in another 2 parameters when calling, after the two actual parameters passed in the bind methoddisplayArgs2("b", "c"); // Output: 12 a b c
Use bind in event handling function:
var obj = { arg1 : 1, attach: function(){ //var self = this; Normal method to pass this $('xxx').on('click',function (event) { (this.arg1);//If this is not bound, this in the callback function often refers to the target element }.bind(this)); //Bind this using bind method } }
Use bind() method to overwrite the slice() method:
var _Slice = ; var slice = (_Slice); slice(…);
bind() is compatible with Ie5~ie8 processing
if (!) { = function(context) { var self = this, // The target function that calls the bind method args = arguments; return function() { (context, (args, 1));// Use apply when the number of parameters is uncertain } } }
Generally, this setTimeout() points to a window or global object. When using the class method, this needs to point to the class instance, you can use bind() to bind this to the calling object without passing it into self.
this
This object is bound based on the execution environment of the function when the function is running: in a global function, this is equal to window, and when the function is called as a method of an object, this is equal to that object.
How to judge: This has nothing to do with where the definition is. When the function is running, if there is an . operator, this refers to the object before.; if not, this refers to window. If the new keyword is called, it refers to a new object. When there is apply/call/bind, it refers to the first parameter.
/*Example 1*/ function foo() { ( ); } var obj2 = { a: 42, foo: foo }; var obj1 = { a: 2, obj2: obj2 }; obj1.(); // 42;When the foo function is called, it itself is owned by obj2/*Example 2*/ function foo() { ( ); } var obj = { a: 2, foo: foo }; var bar = ; // bar refers to the foo function itselfvar a = "global"; // Properties of global objectsbar(); // "global" ;
In an HTML DOM event handler, this always points to the DOM node to which the handler is bound.