Preface
IntroductionFirst of all, we all know that this is a keyword in the Javascript language. It represents an internal object that is automatically generated when the function is running and can only be used within the function. The value of this will change depending on where the function is used. However, there is a general principle, that is, the direction of this cannot be determined when the function is defined. Only when the function is executed can it be determined who this actually points to. In fact, this ultimately points to the object that calls the function in which it is located. So let’s explore this issue step by step. Explore Onefunction a() { var user = "Steamed Bighead Carp"; console.log(this.name); //undefined console.log(this); //Window } a(); window.a(); //The two results are the same As we said above, this ultimately points to the object that calls the function in which it is located. Here, a is actually pointed out by the window object. Exploration 2var obj = { name: 'Steamed Fathead Fish', f1: function () { console.log(this.name); //Steamed bighead carp} }; obj.f1(); It is important to emphasize again that the direction of this cannot be determined when the function is defined. Only when the function is executed can we determine who this is pointing to. In this example, the f1 function where this is located is called by the obj object, so this here points to the obj object. Exploration ThreeIf you want to fully understand this, you must look at the following examples. var obj = { a: 5, b: { a: 10, fn: function () { console.log(this.a); //10 } } }; obj.b.fn(); Doesn't it mean that this ultimately refers to the object that calls the function in which it is located? Why doesn't this point to the obj object? Three points need to be added here:
Seeing this, I believe everyone has basically grasped the principle of this pointing. Let me repeat it again: the pointing of this cannot be determined when the function is defined. Only when the function is executed can we determine who this actually points to. In fact, this ultimately points to the object that calls the function in which it is located. Here are some different usage scenarios for thisConstructor (new keyword) case function Student() { this.name = 'Steamed Fathead Fish'; } var s1 = new Student(); console.log(s1.name); // Steamed bighead carp The reason why object s1 can point to the name in the function Student is because the new keyword can change the pointer of this to point to object s1. // The execution process of the new keyword 1. Create an empty object in the function body. 2. Let the current this point to this empty object. 3. Add key-value pairs to the currently empty object through this. 4. Return the object with all key-value pairs added to the external variable. The this pointer in the timer var num = 0; function Obj() { this.num = 1; this.getNum1 = function () { console.log(this.num); }; this.getNum2 = function () { setInterval(function () { console.log(this.num); }, 1000); }; } var o = new Obj(); o.getNum1();//1 (o.num) o.getNum2();//0 (window.num) The reason why the value of Solution: The function where var num = 0; function Obj() { this.num = 1; this.getNum1 = function () { console.log(this.num); }; this.getNum2 = function () { setInterval(function () { console.log(this.num); }.bind(this), 1000);//Use bind to bind this to this function}; } var o = new Obj(); o.getNum1();//1 (o.num) o.getNum2();//1 (o.num) explain:
According to the principles: Before using the The above is a detailed explanation of the this pointing problem in JavaScript. For more information about JavaScript this pointing, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: MySQL optimization connection optimization
Table property settings that work well: Copy code ...
Table of contents Preface Cause analysis and solu...
JS running trilogy js running code is divided int...
Table of contents Dirty pages (memory pages) Why ...
Preface This article mainly introduces how to sta...
When the same function and HTML code are used mul...
Refer to the official documentation here for oper...
After the container is started Log in to admin fi...
Table of contents 01 Introduction to GTID 02 How ...
Table of contents 1. Introduce cases 2. View the ...
How to check the file system type of a partition ...
Table of contents Preface The role of render Rend...
MySQL will automatically create a database named ...
Preface This article introduces a simple BAT scri...
Encapsulate a navigation bar component in Vue3, a...