Preface
Using properties inside objects in methods inside objects is a very common requirement. However, JavaScript's scope mechanism does not support this. Based on this requirement, JavaScript has another set of this mechanism.
This is bound to the execution context, which means that there is this in each execution context. The execution context is mainly divided into three types: global execution context, function execution context and eval execution context, so there are only these three corresponding this - this in the global execution context, this in the function, and this in eval.
This in the global execution context
Global execution contextthis
It meanswindow
object's. This is alsothis
and scope chainThe only intersection point, the bottom end of the scope chain containswindow
Object, in the global execution contextthis
It also points towindow
Object.
This in the function execution context
function foo() { (this); } foo();
Execute this code, and the printed one is alsowindow
object, which indicates that a function is called by default, whose execution contextthis
It also points towindow
object's.
Generally, there are three ways to set the function execution context.this
value:
1. Set the function's call method
let bar = { myName: "GeekBang", test1: 1, }; function foo() { = "Geek Time"; } (bar); (bar); (myName);
Execute this code and you will findfoo
Internal functionthis
Already pointed tobar
Object, because by printingbar
The object can be seenbar
ofmyName
The attribute has changed from "GeekBang" to "Geek Time", and is printed in the global execution context.myName
, the JavaScript engine prompts that the variable is undefined. In fact, exceptcall
Method, you can also usebind
andapply
Method to set the function execution contextthis
。
2. Call method settings through object
var myObj = { name: "Geek Time", showThis: function () { (this); }, }; ();
Execute this code, you can see the final outputthis
The value refers tomyObj
of. So, you can conclude that using an object to call a method inside it,this
It points to the object itself.
Next, we slightly change the calling method andshowThis
Assign to a global object and then call the object, the code is as follows:
var myObj = { name: "Geek Time", showThis: function () { = "GeekBang"; (this); }, }; var foo = ; foo();
Execute this code and you will findthis
Pointing to the globalwindow
Object.
in conclusion:
Call a function in the global environment, the function inside thethis
Point to global variableswindow
。
A method inside is called through an object, and the execution context of the method isthis
Point to the object itself.
3. Set it through the constructor
function CreateObj() { = "Geek Time"; } var myObj = new CreateObj();
When executednew CreateObj()
At the time, the JavaScript engine did the following four things:
First create an empty objecttempObj
Then callMethod and
tempObj
Ascall
The parameters of the method areCreateObj
When the execution context is created, itthis
Just point totempObj
Object
Then executeCreateObj
Function, at this timeCreateObj
Function execution contextthis
Pointed totempObj
Object
Last returntempObj
Object
var tempObj = {}; (tempObj); return tempObj;
In this way,new
The keyword has built a new object and the constructor isthis
In fact, it is the new object itself.
This design defects and response solutions
1. This in nested functions will not be inherited from outer functions
var myObj = { name: "Geek Time", showThis: function () { (this); function bar() { (this); } bar(); }, }; ();
After executing this code, you will find the functionbar
In-housethis
Pointing to the globalwindow
Objects, and functionsshowThis
In-housethis
Pointing tomyObj
Object.
This problem can be solved by a small trick, such asshowThis
Declare a variable in the functionthat
Used to savethis
, and thenbar
Used in functionsthat
. In fact, the essence of this method is tothis
The system is converted into a scoped system.
In fact, you can also use the arrow function in ES6 to solve this problem:
var myObj = { name: "Geek Time", showThis: function () { (this); var bar = () => { (this); }; bar(); }, }; ();
This is because the arrow function in ES6 does not create its own execution context, so the arrow function inthis
Depends on its external function.
2. This in normal functions points to the global object by default window
In actual work, we do not want the function execution contextthis
By default, it points to the global object, because this will break the boundaries of the data and cause some misoperations. If you want to make the function execute in the contextthis
The best way to point to an object is tocall
Method to display the call.
You can set up JavaScriptStrict modeTo solve (add in the first line"use strict";
). In strict mode, a function is executed by default, and its function's execution contextthis
The value isundefined
。
The above is the detailed explanation of this usage from the perspective of JavaScript execution context. For more information about JavaScript execution context, please pay attention to my other related articles!