The spread operator allows an expression to be expanded at some point. The spread operator can be used where there are multiple parameters (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignments). let obj1 = { value1: 1, value2: 2 }; let obj2 = {...obj1 }; console.log(obj2); // {value1: 1, value2: 2} The above usage is actually equivalent to obj2 = {value1: 1, value2: 2} The difference between the writing of the spread operator and the writing of direct assignment of let obj1 = { attri1: [3, 6, 0], attri2: 4, attri4: 5 }; let obj2 = {...obj1 }; obj2.attri2 = 888; obj2.attri1[0] = 7; console.log('obj1:', obj1); console.log('obj2:', obj2); Application of the spread operator1. Using the spread operator in a function function test(a, b, c){}; let arr = [1, 2, 3]; test(...arr); 2. Using the spread operator in array literals let arr1 = [1, 2]; let arr2 = [...arr1, 3, 4]; // [1, 2, 3, 4] // Using push method let arr1 = [1, 2]; let arr2 = [3. 4]; arr1.push(...arr2); // [1, 2, 3, 4] 3. Used for deconstruction assignment. The expansion operator can only be used at the end of the deconstruction assignment, otherwise an error will be reported. // The spread operator can only be used at the end of a destructuring assignment. let [a, b, ...c] = [1, ,2, 3, 4] console.log(a, b, c) // 1, 2, [3, 4] 4. Class array becomes array let oLis = document.getElementsByTagName("li"); let liArr = [...oLis]; 5. Use the spread operator in objects let {x,y,...z}={x:1,y:2,a:3,b:4}; x; // 1 y; // 2 z; // {a:3,b:4} Insert an object into another object: let z={a:3,b:4}; let n={x:1,y:2,...z}; console.log(n); //{x:1,y:2,a:3,b:4} Merge two objects: let a={x:1,y:2}; let b={z:3}; let ab={...a,...b}; console.log(ab); // {x:1,y:2,z:3} This is the end of this article about the spread operator in JavaScript and its application example code. For more relevant js spread operator content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Specific use of lazy loading and preloading in js
>>: Detailed explanation of the steps to create a web server with node.js
Preface: Last Sunday, a senior asked me to help m...
Table of contents 1. The writing order of a compl...
If you’re new to Docker, take a look at some of t...
Effect html <body> <div class="cont...
First post the effect picture: A scroll bar appear...
This article shares the specific code for JavaScr...
Layout part: <div id="slider"> &l...
Table of contents MySQL Truncate usage 1. Truncat...
When making a new version of the configuration in...
1. What is a proxy server? Proxy server, when the...
This tutorial explains how to verify the IP addre...
Table of contents Preface 1. Preparation 2. Insta...
Step 1: Enter the directory: cd /etc/mysql, view ...
I have nothing to do recently, so I tinker with C...
Preface What is state We all say that React is a ...