All cases that Java Script this points to

Source: Internet
Author: User

1. Direct call, point to Global
Console.log (this);
2. Called in the normal function, pointing to the global
function fn () {
Console.log (this);
}
FN ();
3. Constructor normal call, point to Global (constructor is also normal function, can execute normally)
function Student () {
This.name= "Zhangsan";
Console.log (this);
}
Student ();
4. The constructor creates an instance object from the new call, pointing to the instance object
var x=0;
function Student (name,x) {
This.name=name;
This.x=x;
Console.log (this.x);
}
var zhangsan=new Student ("Zhangsan", 1);
var lisi=new Student ("Lisi", 2);
5. Object (JSON creation) inside the method call, point to this object
var object1={
Name: "Zhangsan",
Show:function () {
Console.log (this);
}
}

Object1.show ();
6. The method call inside the object (created by object), pointing to the object
var object2 =new Object ();

Object2.name= "Zhangsan";
Object2.show=function () {
Console.log (this);
}

Object2.show ();
7. The method call inside the object (created by the constructor), pointing to the object
function Student () {
This.name= "Zhangsan"
This.show=function () {
Console.log (this);
}
}
var object3=new Student ();

Object3.show ();

All cases that Java Script this points to

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.