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
The rewrite module is the ngx_http_rewrite_module...
There are many differences between IE6 and IE7 in ...
1. Construction 1. Prepare htpasswd.txt file The ...
Table of contents Case 1: Case 2: Case 3: To summ...
Use CSS3 to achieve cool radar scanning pictures:...
This article example shares the specific code for...
Solution to the problem of automatic disconnectio...
introduction Have you ever encountered a situatio...
Preface When I was typing my own personal blog, I...
Table of contents 01 What is the essence of a con...
Table of contents 1. Overview 2. Download the Ngi...
Chinese documentation: https://router.vuejs.org/z...
1. Installation apt-get install mysql-server requ...
1. Vertical table and horizontal table Vertical t...
Table of contents Is setState synchronous or asyn...