3 points so that you completely get to know this

Many students who do not know js inside of this in the end is? I do not know this in the end how to use it? , Today we summarize the usage of this.

 

1: this mechanism

  Js enter the function which can access a special "Parameters" is called this. And this many high-level languages ​​(java, c #, c ++) is not the same, js of this is what you pass, that is what, this other high-level language, is an instance of the current class, but js is not the same, js of this is more like an argument, what you pass, it is what.

2: this use doctrine

  From the above, get a principle, you can use this function in the inside, but must know before using currently this is what object, if you are unsure, do not use.

3: How to find out who is this?

  Use this function in which, at the time of the transfer function is called, the call functions deliver this in three ways:

(1) transmitting display:  

Function a (parameter 1, parameter 2) ... {...};  

a.call ( "this object function", parameter 1, parameter 2), into a function, using this, then this is passed outside the object, e.g. a.call ( "hello", parameter 1, parameter 2); enter after a function, this refers to the "hello" string object. a.call ({uname: "blake"}, parameter 1, parameter 2); into a function, this means that {uname: "blake"} object instance;

  (2) implicitly passed this:

Recall that the first table ({key: value}). Access data members, members of the table name, the member may be a function of the object, so when called, table function member name (parameter 1, parameter 2), then the use of this approach. when entering the object inside the function, using this example refers to the table in function inside out;

 (3) enforce this binding and mandatory trap bindings

Mandatory binding function object is a mandatory binding good this object so that the object that you use this function to bind to the inside,

E.g. var a = function (parameter 1, parameter 2) {} .bind ( "hello");

a (parameter 1, parameter 2); into the function this refers to the binding force "hello", includes a binding force has the highest priority, with a binding force, the binding force in the this main force binding> = display / implicit. There is a mandatory binding overlooked the fact that 90% of people, that is:

   var a = function() {… …};

   var b = a.bind ( "object");

Will, a and b are the same function object it? Interested students to test themselves. ^ _ ^

Guess you like

Origin www.cnblogs.com/blakehuangdong/p/11329671.html