Summarize
Global EnvironmentRegardless 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
ConstructorWhen 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 FunctionsWhen 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 methodsWhen 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()
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:
|
<<: How to install elasticsearch and kibana in docker
>>: The difference between distinct and group by in MySQL
How to check the file system type of a partition ...
Table of contents origin Environmental Informatio...
<br />For some time, I found that many peopl...
Business requirements One of the projects I have ...
Method 1: Use the SET PASSWORD command mysql> ...
Scenario 1: Due to server restrictions, only one ...
Table of contents How to operate Operation proces...
hash mode (default) Working principle: Monitor th...
The original code is this: <div class='con...
CentOS8 was released a few days ago. Although it ...
Table of contents 1. Technical Overview 2. Techni...
This article example shares the specific code of ...
The first solution is to push the image to a publ...
How to allow remote connection in MySql To achiev...
Drop-down box, text field, file field The upper p...