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
Recently, I have done a simple study on the data ...
background In the group, some students will ask r...
Table of contents 1. Command 2. docker-compose.ym...
When doing a project, it is inevitable to encount...
HTML imitates the Baidu Encyclopedia navigation d...
Table of contents background analyze Data simulat...
Traditionally, developers create properties in Ja...
Running Docker requires root privileges. To solve...
Effect demo.html <html> <head> <me...
iOS 1. URL scheme This solution is basically for ...
I recently started learning Linux. After reading ...
What is a web page? The page displayed after the ...
Preface It's a cliché. Here I will talk about...
Prerequisite: Save the .frm and .ibd files that n...
This article mainly introduces the case of Vue en...