object, the use of this

Source: Internet
Author: User

The use of this:

A. This in the constructor
1. The constructor is used as a constructor function:

This in the constructor or in the prototype of the function, pointing to the object created by new
(which object calls the constructor, then this points to which object).
Example: function Person0 () {
THIS.name = "Zhangsan0";
This.age = 20;
This.sayname = function () {
Console.log (this);
Console.log (this.name);
};
Person0.prototype.sayage = function () {
Console.log (this);
Console.log (This.age);
};
}
var p0 = new Person0 ();
P0.name = "Zhangsan1";
P0.age = 10;
P0.sayname ();//return zhangsan1, not zhangsan0
P0.sayage (); Returns 10 instead of 20

From the above see:
1. Person0, through new, created the P0 object;
2. This and prototype in Person0 are pointed to p0;
3. P0 invokes various properties or methods within the function, so this is a point to p0;


2. Constructors are used as normal functions:
Instead of creating the object with new, the constructor is called directly, which is the way to use the constructor as a normal function;
The This in the normal function points to the Window object;
Example: function Person1 () {
THIS.name = "Zhangsan0";
This.age = 20;
This.sayname = function () {
Console.log (this);
Console.log (this.name);
};
Sayname ();
}
Person1 (); Returns the Window object; return zhansan0

From the above see:
1. This in the constructor of person refers to the window object;
2. Because this points to the Window object, all the THIS.name or this.age in the constructor of the person is equivalent to defining two name and age attributes for the window and assigning values of zhangsan0 and 20, So Console.log (this.name) This sentence returned is zhangsan0;


Two. function as a property of an object:
1. The upper level of the function is an object, not a function: then this object of the function is pointing at its upper level.
Example: var x = 10;
var Person3 = {
X:20,
Sayname:function () {
Console.log (this);
Console.log (this.x);
}
};
Person3.sayname (); Returns the Person3 object; return 20

From the above see:
1. The upper level of the Sayname function is the Person3 object, so this object in the Sayname function is directed to Person3;
2. So Console.log (this), this sentence returns the Window object;
3. So Console.log (this.x); This sentence returns the value of the X attribute inside the Person3;

2. The upper level of function A is function B, the upper level of function B is Object C, then the this in function a points to a Window object (which is considered to be a normal function), and this in function B points to object C.
Example: var x = 10;
var person4 = {
X:20,
Sayname1:function () {
Console.log (this); Returns the Person4 object
Console.log (this.x); Returns 20
Sayname2 = function () {
Console.log (this); Returns the Window object
Console.log (this.x);//Return 10
};
Sayname2 ();
}
};
Person4.sayname1 ();
return: Person4, 20; Window object, 10.
From the above see:
1. The upper level of the Sayname2 function is the SAYNAME1 function, so the this in Sayname2 points to the Window object;
2. The upper level of the SAYNAME1 function is the Person4 object, so this point in Sayname1 is directed to the Person4 object;
3. Output see the comments in the above code


Three. This in global & normal functions: All points to the Window object
1. This in the global:
Example: console.log (this = = = window);//return True
See: Returns TRUE, indicating that this in the global is pointing to the Window object

2. This in the normal function:
Example: var a = 20;
function Person5 () {
var a = 10;
Console.log (THIS.A);
}
Person5 (); Returns 20
It is seen from the above: the value of the global variable A is returned, not the value of variable A in the function, so this in the normal function points to the Window object.

Four. Invocation of the call method: this in the function that invokes the call method points to the object in the incoming calls method.
Example: function Person6 () {
Console.log (this.name);
}
var P1 = {
Name: "Zhangsan2"
};
Person6.call (p1); Back to Zhangsan2
From the above see:
1. There is a statement in the PERSON6 function for Console.log (this.name);
2. Define the name attribute in the object P1 and assign the value;
3. When the call method is called by the Person6 constructor, this point in the function refers to the P1 of the object that is passed in to calls, so the value of the P1 Name property is output.




object, the use of 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.