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

Detailed explanation of Nginx's rewrite module

The rewrite module is the ngx_http_rewrite_module...

Tips for using DIV container fixed height in IE6 and IE7

There are many differences between IE6 and IE7 in ...

How to use Docker to build a pypi private repository

1. Construction 1. Prepare htpasswd.txt file The ...

Several situations that cause MySQL to perform a full table scan

Table of contents Case 1: Case 2: Case 3: To summ...

CSS3 radar scan map sample code

Use CSS3 to achieve cool radar scanning pictures:...

Element UI table realizes drop-down filtering function

This article example shares the specific code for...

Briefly understand the MYSQL database optimization stage

introduction Have you ever encountered a situatio...

How to prevent users from copying web page content using pure CSS

Preface When I was typing my own personal blog, I...

Analysis of the principles of docker containers

Table of contents 01 What is the essence of a con...

Teach you how to quickly install Nginx in CentOS7

Table of contents 1. Overview 2. Download the Ngi...

Vue Router vue-router detailed explanation guide

Chinese documentation: https://router.vuejs.org/z...

How to install and configure MySQL and change the root password

1. Installation apt-get install mysql-server requ...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...