Detailed explanation of the this pointing problem in JavaScript

Detailed explanation of the this pointing problem in JavaScript

Summarize

  • Global environment ➡️ window
  • Normal function ➡️ window or undefined
  • Constructor ➡️ Constructed instance
  • Arrow function ➡️ this in the outer scope when defined
  • Object methods ➡️ The object
  • call(), apply(), bind() ➡️ The first parameter

Global Environment

Regardless of strict mode, this refers to the window object.

console.log(this === window) // true
// Strict mode 'use strict'
console.log(this === window) // true

Ordinary functions

  1. Normal Mode
    • this refers to the window object
    • function test() {
        return this === window
      }
      
      console.log(test()) // true
      
  2. Strict Mode
    • this value is undefined
    • // Strict mode 'use strict'
      
      function test() {
        return this === undefined
      }
      
      console.log(test()) // true
      

Constructor

When a function is used as a constructor, this points to the constructed instance.

function Test() {
  this.number = 1
}

let test1 = new Test()

console.log(test1.number) // 1

Arrow Functions

When the function is an arrow function, this refers to the this value in the previous scope when the function is defined.

let test = () => {
  return this === window
}

console.log(test()) // true
let obj = {
  number: 1
}

function foo() {
  return () => {
    return this.number
  }
}

let test = foo.call(obj)

console.log(test()) // 1

Object methods

When a function is used as a method of an object, this refers to the object.

let obj = {
  number: 1,
  getNumber() {
    return this.number
  }
}

console.log(obj.getNumber()) // 1

call(), apply(), bind()

  • When calling the call() or apply() method of a function, the this of the function points to the first parameter passed in.
  • When the bind() method of a function is called, the this of the returned new function points to the first parameter passed in.
let obj = {
  number: 1
}

function test(num) {
  return this.number + num
}

console.log(test.call(obj, 1)) // 2

console.log(test.apply(obj, [2])) // 3

let foo = test.bind(obj, 3)
console.log(foo()) // 4

This is the end of this article about the detailed case of this pointing problem in JavaScript. For more related content about this pointing problem in JavaScript, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript basics of this pointing
  • Detailed explanation of this pointing in JS arrow function
  • Detailed explanation of this reference and custom properties 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

<<:  How to install elasticsearch and kibana in docker

>>:  The difference between distinct and group by in MySQL

Recommend

WeChat applet uniapp realizes the left swipe to delete effect (complete code)

WeChat applet uniapp realizes the left swipe to d...

MySQL slow query operation example analysis [enable, test, confirm, etc.]

This article describes the MySQL slow query opera...

Div adaptive height automatically fills the remaining height

Scenario 1: Html: <div class="outer"...

Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

The following are all performed on my virtual mac...

A brief introduction to MySQL InnoDB ReplicaSet

Table of contents 01 Introduction to InnoDB Repli...

Detailed explanation of the abbreviation of state in react

Preface What is state We all say that React is a ...

A Deep Dive into JavaScript Promises

Table of contents 1. What is Promise? 2. Why is t...

JS calculates the probability of winning based on the prize weight

Table of contents 1. Example scenario 1.1. Set th...

Some questions about hyperlinks

<br />I am very happy to participate in this...

Summary of the three stages of visual designer growth

Many people have read this book: "Grow as a ...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

How to modify the forgotten password when installing MySQL on Mac

1. Install MySQL database on mac 1. Download MySQ...

jQuery plugin to achieve image suspension

This article shares the specific code of the jQue...

Core skills that web front-end development engineers need to master

The content involved in Web front-end development...

Example of MySQL auto-increment ID exhaustion

Display Definition ID When the auto-increment ID ...