This article mainly introduces the relevant information of javascript this in detail. Friends who need it can refer to it
The value of this is determined at runtime
What exactly this in JS represents is determined according to the context when the program is running, and can be divided into the following situations.
1. This in the global scope
In the global scope, this points to the window object.
console.log(this);//指向window对象 this.x = 5//在全局作用域内创建一个x //与this.x = 5的等价情况: //var x = 5; //x = 5;
Executing var x=5 in the global scope actually creates an attribute x for the window object and makes it equal to 5.
If you do not add var when defining a variable, JS will consider the variable to be a global variable and treat it as a property of the window object.
2. this in the function
There are two types of functions in JS. The function that is called directly is called a normal function, and the function that creates an object through new is called a constructor. .
2.1 This in the constructor
This in the constructor points to the object it creates, such as:
function Person(name){ this.name = name;//this指向该函数创建的对象person } var person = new Person("chaimm");
2.2 This
in the ordinary function This of the ordinary function points to the window object.
If the Perosn function is executed directly in the above example, then this represents the window object, so a global name will be created after the function is executed.
function Person(name){ this.name = name;//this指向window } Person("chai");//当作普通函数执行,this指向window对象
3. This in the object
This in the object points to the current object, such as:
var person = { name : "chaimm", getName : function(){ return this.name; } }
In the above code, this points to the function getName belongs to Object.
However, if there is a function nested in the function of an object, the this of this function points to the window, not its outer object.
var person = { name : "chaimm", setName : function(name){ (function(name){ this.name = name; //此时this并不代表person对象,而是代表window对象 })(name); } }
In the above example, there is a getName function in the person object, and there is a function inside the getName function. This inside the function points to the window object, not the person object. This is a bug in JS! Generally, the following processing is done to avoid this bug:
var person = { name : "chaimm", setName : function(name){ var thar = this;//将this赋给that (function(name){ that.name = name;//此时that指向person对象 })(name); } }
In the first-level function of the person object, we assign this to the local variable that, and then use that in the second-level function. At this time, that points to the person. Object, which can operate on the attributes of person.
Note: If a function in an object is assigned to a variable, and then the function is called through the variable, this in the function points to the window, not the object, as shown below:
var person = { name : "chaimm", getName : function(){ return this.name; } } //将getName函数赋给一个新的变量 var newGetName = person.getName; //通过新的变量调用这个函数,这个函数中的this将指向window newGetName();//若全局作用域中没有name,则将返回undefined
4. Use the call and apply functions to cheat this
Both of these functions can manually specify which object this inside the called function points to.
//定义一个构造函数 var Person = function(name){ this.name = ""; this.setName = function(name){ this.name = name; } } //创建两个对象 var personA = new Person("A"); var personB = new Person("B"); //使用personA的setName函数去修改personB的name属性 personA.setName.apply(personB,["C"]);
apply usage
Object A.Function name.apply(Object B, parameter list);
When object B is passed as the first parameter of apply When applying, this in the function of object A points to object B. At this time, the operation of this function on object A will be applied to object B, thus realizing using object A to call the function of object B.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Some learning summary of Javascript this
The six hurdles of JavaScript This
Detailed introduction to what JavaScript this points to? (Pictures and text)
The above is the detailed content of Detailed explanation of javascript this (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!