Summarize1. Similarities
2. Difference
call() method
/* Normal mode */ let obj = { sum(a, b) { console.log(this) return a + b } } // Execute the apply and bind methods of the sum function. The printed this is the same as below. obj.sum.call() // Print window obj.sum.call(undefined, 1, 2) // Print window obj.sum.call(null, 1, 2) // Print window /* Strict mode */ 'use strict' // Execute the apply and bind methods of the sum function, and print this as below obj.sum.call() // Print undefined obj.sum.call(undefined, 1, 2) // prints undefined obj.sum.call(null, 1, 2) // prints null Simulation Implementation1. Key points
2. In the simulation implementation of call(), apply(), and bind() methods, when the first parameter is not passed or undefined or null is passed, unified processing is done here in JS normal mode and strict mode, that is, this inside the target function points to the window object. 3. The code is as follows Function.prototype.myCall = function (context, ...args) { if (context === undefined || context === null) { context = window } // The following line is the core code context.fn = this const result = context.fn(...args) delete context.fn return result } let obj1 = { basicNum: 1, sum(a, b) { console.log(this) return this.basicNum + a + b } } let obj2 = { basicNum: 9 } console.log(obj1.sum.call(obj2, 2, 3)) // 14 console.log(obj1.sum.myCall(obj2, 2, 3)) // 14 apply() methodCalling the apply() method will immediately execute the target function and change the reference of this inside the function. This points to the first parameter of the method. The second parameter is a parameter array or an arguments object. Each parameter represented by each array element or arguments object will be passed in one by one as the parameter of the target function. Simulation Implementation1. Key points
2. The code is as follows Function.prototype.myApply = function (context, args) { if (context === undefined || context === null) { context = window } // The following line is the core code context.fn = this const result = context.fn(...args) delete context.fn return result } console.log(obj1.sum.apply(obj2, [2, 3])) // 14 console.log(obj1.sum.myApply(obj2, [2, 3])) // 14 bind() method
Simulation Implementation1. Key points
2. The code is as follows Function.prototype.myBind = function (context, ...initArgs) { if (context === undefined || context === null) { context = window } // Cache this value const _this = this return function (...args) { // The following line is the core code context.fn = _this const result = context.fn(...initArgs, ...args) delete context.fn return result } } console.log(obj1.sum.bind(obj2, 2)(3)) // 14 console.log(obj1.sum.myBind(obj2, 2)(3)) // 14 Related knowledge points
This is the end of this article about the detailed case analysis of JavaScript function call, apply and bind methods. For more relevant content about JavaScript function call, apply and bind methods, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A tutorial for beginners to install and log in to mysql-8.0.19-winx64 (must-read for beginners)
>>: How to use the realip module in Nginx basic learning
Table of contents Preface 1. Environment Configur...
Table of contents Preface Bubble Sort Basic Algor...
Table of contents 1- Error details 2-Single Solut...
After the server where Docker is located has been...
introduction When I was learning more about datab...
Docker underlying technology: The two core techno...
Requirements: The PC side and the mobile side are...
Due to your company standards, you may only allow...
Compared with fdisk, parted is less used and is m...
As shown below: Mainly execute authorization comm...
Word MySQL 8.0 has been released for four years s...
There is a project developed on Mac, and the pack...
1 Pull the image from hup docker pull nginx 2 Cre...
Recently, when I was learning jQuery, I came acro...
This article uses examples to explain the concept...