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
MySQL database storage location: 1. If MySQL uses...
The picture is used as the background and the lin...
Table of contents Short Polling Long-Polling WebS...
Table of contents Preface How to switch between m...
Lock classification: From the granularity of data...
It seems that the mysql-sever file for installing...
1. Background The following two problems are enco...
When using Flex layout, you will find that when a...
When I was looking at some CSS3 animation source ...
MySql Index Index advantages 1. You can ensure th...
In an article a long time ago, I talked about the...
Transition document address defines a background ...
Introduction MySQL should be a very common databa...
Table of contents 1. Check the status of the serv...
MySql uses joined table queries, which may be dif...