JS Love and hate revenge say this

Source: Internet
Author: User

The original: JS Love hate revenge say this

This I believe that everyone in the writing code will encounter, but how to use good this, it is still a bit difficult to estimate, although you sometimes you will use, but he is in the specific scenes of what the meaning of the? Maybe this is not very clear to you. This will be a lot of problems in your use of the process, so today we summarize this, exactly this? Is he really that hard to imagine?

Can actually be summed up in a sentence:this refers to the object that called the function

So we can summarize the four invocation scenarios for this:

(1) Method mode: Simple is to use the point expression or subscript expression to call here is bound to have an object in this case, the binding of this occurs when the call to bind the nature of the object to tune it.

So let's take a look at the following example: Also in the blog Park I feel a more classic example:

var x = 2; function Test () {    alert (this. x);} Test (); // 2 here Text (): Out of the data is 2 actually we can understand:
var window.x = 2; function Test () {    Alert (window. x);//}window.test ();//2
var x = 2
function
{this.x = 0

alert (x);//0
In a different way.
var window.x = 2
function
{window. x = 0

alert (window.x); // 0

Can you understand the first way of the method.

2) function pattern: This is simpler, the function name plus the call operator (' () '). But be careful, this one is tied to a global object, no matter where you write it. (Can be understood as   you don't show me the    I'll add a global object to it myself)
when a function is called by a function instead of a method call, the This keyword refers to the global object. It is easy to confuse that when a nested function (as a function) is called in a contained function, and this contained function is called as a method, this is also true: the This keyword has a value in the included function, but it does (less intuitively) refer to the inner global object of the nested function body.

 var  a = ' global ' ;  var  obj = {a:  ' local '   
var a = ' global '; var obj = {    A: ' Local ',    test:function() { alert (THIS.A);//local Actually, we can write a. Lert (OBJ.A) }};obj.test (); Can you understand that?     

In response to the above questions, we propose the following outcome plan:

 var  a = ' global ' ;  var  obj = {a:  ' local '  function   Test1 () {alert (THAT.A); // local      Test1 (); }};obj.test ();  

3) constructor invocation pattern   A sentence is a new call with new   new when this is bound to a newer object   better understanding
The 1.new operator must be followed by a function call. New creates an object without any properties, and then calls the constructor to pass the new object as the value of the This keyword.
2. Constructors typically do not return a value. They initialize the object that is passed as the value of this, and there is no return value. But a construct is allowed to return an object value, and if it does, the returned object becomes the value of the new expression. In this case, the object that is the value of this is discarded.

var function (String) {    this. Name =function  ()}    {returnthis  . Name}varnew person (' Lin '); Myperson.getname (); If you bring a new to call before a function, A new object that is hidden to the prototype member of the function is created, and this is bound to the new object. The new prefix also alters the behavior of the return statement. 

But what exactly is the principle of construction? Looking a little dizzy, huh?

 function   Newoperator (Constr, args) {  var  thisvalue = object.create (Constr.prototype); //  (1)  var  result = Constr.apply (Thisvalue, args);  if  (typeof  result = = = ' object ' && result!== null  ) { return  result; //  (2)      

4) Apply,call Call pattern Apply,call is the method of the function object, and you want to bind who to the this to directly pass it as the first parameter to the apply or the calling.

var a= {n: ' Lin '}; var function () {    returnthisnull)); // Output Lin

Hope the garden friends correct me. Keep on trying to refuel. Next closure.

I'm going to write down a special study of this one today and don't say it. Continue to work hard ... Hope the Garden friends correct me

JS Love and hate revenge say this

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.