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