JavaScript method--bind ()

Source: Internet
Author: User

Bind method, as the name implies, is the meaning of binding, in the end how to bind then how to use it, the following is to say I understand this method.

Grammar

Fun.bind (This,arg1,arg2,...)

The bind () method creates a new function called a binding function, which is called by the fun method in the This environment

The method can pass in two parameters, the first parameter as this, the second and later arguments as the function parameters call

Instance

1. Create a binding function

THIS.A = 1;
var module = {
A:2,
Geta:function () {
return THIS.A;
}
};
Module.geta ();//2

var getA1 = Module.geta;
The Geta is called externally, at which point this points to the global object
GetA1 ();//1

Then bind the GetA1 method to the module environment.
var getA2 = geta1.bind (module);
GetA2 ();

Press CTRL + C to copy the code<textarea></textarea>Press CTRL + C to copy the code

As can be seen from the above example, the reason for creating a binding function is that when we invoke certain functions to be called in a particular environment, we are going to put the function in a specific environment, that is, bind the function to a specific desired environment using bind.

2. Let the function have preset parameters

Use the bind () method to have the default initial parameters for the function, which will be the first, and the arguments passed to the binding function will follow them.

1 Function List () {2     //Let class array arguments has an array of methods slice, this function implements a simple to convert the class array to the Group 3     return Array.prototype.slice.call ( arguments);  4} 5  6 list (//[1,2,3] 7  8//bind a preset parameter to list 4  9 var list1 = List.bind (undefined,4); list1 (List1);//[4,1,2,3]

Use of 3.setTimeout

Normally, this will point to the global object when calling settimeout, but we need to point to an instance of the class when using the method of the class, so to put this, bind the callback function to facilitate the use of the instance

1 function Fun1 () {  2   this.name = 1; 3  } 4 Fun1.prototype.fun2 = function () {5   window.settimeout (this.fun 3.bind (this), 1000); 6  } 7 Fun1.prototype.fun3 = function () {8     console.log (' name: ' +this.name);//name:1 9}10 var fun = new Fun1 (); Fu N.fun2 ();

4. Quick Method--converting an array of classes into arrays

The first method is to use the Apply method

1 function fun1 () {2     var slice = array.prototype.slice;3     return slice.apply (arguments); 4}5 6 fun1 (n/a);//[ [+]

The second approach is to use the call method with the Bind method

function fun2 () {    var unboundslice = Array.prototype.slice;    Bind the call method of the function to the array slice method, and then pass the parameter    var slice = Function.prototype.call.bind (unboundslice) to the call method;    return slice (arguments);} Fun2 (//[1,2,3);

JavaScript method--bind ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.