JavaScript basics of this pointing

JavaScript basics of this pointing

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.

this

As usual, let’s look at the code first:

Method

function test(){
    console.log(this);
}

insert image description here

In the object

function test(){
    console.log(this);
}

insert image description here

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.

insert image description here

Hidden this

In 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);

insert image description here

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.

insert image description here

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);

insert image description here

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);

insert image description here

Strict Mode

This 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;
}

insert image description here

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.

Summarize

This 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:
  • Detailed explanation of this pointing in JS arrow function
  • Detailed explanation of this reference and custom properties in JavaScript
  • Detailed explanation of the this pointing problem in JavaScript
  • Detailed explanation of function classification and examples of this pointing in Javascript
  • Detailed explanation of this pointing problem in JavaScript function
  • Detailed explanation of the this pointing problem of JavaScript prototype objects

<<:  Solution to CSS flex-basis text overflow problem

>>:  Understand the rendering process of HTML pages in preparation for learning front-end performance optimization

Recommend

How does MySQL connect to the corresponding client process?

question For a given MySQL connection, how can we...

Detailed explanation of CSS label mode display property

The code looks like this: <!DOCTYPE html> &...

How to reference jQuery in a web page

It can be referenced through CDN (Content Delivery...

JS array deduplication details

Table of contents 1 Test Cases 2 JS array dedupli...

Detailed explanation of the use of the <meta> tag in HTML

In the web pages we make, if we want more people ...

How to implement MySQL master-slave replication based on Docker

Preface MySQL master-slave replication is the bas...

How to prohibit vsftpd users from logging in through ssh

Preface vsftp is an easy-to-use and secure ftp se...

MySQL graphical management tool Navicat installation steps

Table of contents Preface 1. Arrange the installa...

Analysis of the advantages and disadvantages of MySQL stored procedures

MySQL version 5.0 began to support stored procedu...

CentOS8 - bash: garbled characters and solutions

This situation usually occurs because the Chinese...

How to implement paging query in MySQL

SQL paging query:background In the company's ...

Detailed introduction of Chrome developer tools-timeline

1. Overview Users expect the web applications the...