this in JavaScript is also a magical thing. In object-oriented programming (such as Java), it represents a current object reference. However, in JavaScript, this is not fixed, but changes with the running environment. thisAs usual, let’s look at the code first: Methodfunction test(){ console.log(this); } In the objectfunction test(){ console.log(this); } In a method, this refers to the object to which the method belongs. Because the first one is a method on window, window is printed, and the eat method is a Person method, so the object Person is printed. So it can be seen that using this alone in the console represents the global object. Hidden thisIn objects, you can declare them one by one in advance: var Person1 = { name:"Zhang San", age:18 } var Person2 = { name:"Li Si", age:19 } This would be very troublesome to write, so you can learn from the concept of Java class, like this: var Person = function (name, age) { this.name=name, this.age=age } var Person1=new Person("张三",18); var Person2=new Person("Li Si",19); In fact, there is a return this hidden in new. If you don’t use new, you will find that it does not return the newly created object. Now let’s complete it: var Person = function (name, age) { this.name=name, this.age=age return this; } var Person1=new Person("张三",18); var Person2=new Person("Li Si",19); In this way, you can even fake the effect of this: var Person = function (name, age) { var that={}; that.name=name, that.age=age return that; } var Person1=new Person("张三",18); var Person2=new Person("Li Si",19); Strict ModeThis has some magical behavior in strict mode and non-strict mode function test() { return this; } # If "use strict" is added before js, it means strict mode "use strict"; function test() { return this; } This shows that in a function in non-strict mode, the owner of the function is bound to this by default. So the global value can be printed, but in strict mode the function is not bound to this, so this is undefined. SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Solution to CSS flex-basis text overflow problem
question For a given MySQL connection, how can we...
The code looks like this: <!DOCTYPE html> &...
It can be referenced through CDN (Content Delivery...
The following is an introduction to using SQL que...
Table of contents 1 Test Cases 2 JS array dedupli...
In the web pages we make, if we want more people ...
Preface MySQL master-slave replication is the bas...
Preface vsftp is an easy-to-use and secure ftp se...
In the process of team development, it is essenti...
Table of contents Preface 1. Arrange the installa...
MySQL version 5.0 began to support stored procedu...
1. Wireless Run PowerShell and enter the command:...
This situation usually occurs because the Chinese...
SQL paging query:background In the company's ...
1. Overview Users expect the web applications the...