Residual operators…

  • . The rest of the operator can put the last parameter in an array.
let fn = (a, ... arr) = > {
  console.log(arr);
};
fn("sssss".1.3.2.33.22.345);
// [1, 3, 2.33, 22, 345]
Copy the code

Expansion operator…

  • The expansion operator fetches all the elements of an array individually
  • . The expansion operator can pass an array of arguments, and multiple parameters can be received one by one
// The old way of writing
function fn(a, ... arr) {
  sum.apply(null, arr);
}
function sum(a, b, c, d, e) {
  console.log(a, b, c, d, e); //1 3 2.33 22 345
}
fn("sssss".1.3.2.33.22.345);
Copy the code
// Expand the operator values
function fn(a, ... arr) { sum(... arr); }function sum(a, b, c, d, e) {
  console.log(a, b, c, d, e); //1 3 2.33 22 345
}
fn("sssss".1.3.2.33.22.345);
Copy the code
  • The expand operator merges arrays
let arr = [...[1.2.3.4.5.6.7].8. [9.10.11]].console.log(arr);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Copy the code

Deep copy Shallow copy

  • After the deep copy is complete, the two arrays have the same value, and the reference space does not affect each other (changing the value of one array does not affect the other array).
  • Shallow copy The value of the two arrays is equal and the reference space is common. (Changing the value of one array affects the other array.)
// Assign the array to a shallow copy
let b = [1.2.3.6.4.5];
let a = [b];
let c = b;
b[0] = 100;
console.log(c);  
// [100, 2, 3, 6, 4, 5]
Copy the code
// Assign the array to a shallow copy
let b = [1.2.3.6.4.5];
let a = [b];
let c = b;
b[0] = 100;
console.log(c);  
// [100, 2, 3, 6, 4, 5]
Copy the code
  • The slice method copies one layer of the array as a deep copy, and two or more layers of the array as shallow copies (the first layer is both deep copies)
// Slice indicates a deep copy
let b = [1.2.3.6.4.5];
let a = [b];
let c = b.slice(0);
b[0] = 100;
console.log(c);
// [1, 2, 3, 6, 4, 5]
Copy the code
// Slice Indicates a shallow copy. The first layer is still a deep copy
let b = [1.2.3.6.4.5];
let a = [b];
let c = a.slice(0);
a[0] = ['xxx'.5.6.4.8.8];
b[0] = 100;
console.log(c);
// [[100, 2, 3, 6, 4, 5]]
Copy the code

Expansion operator… Shallow copy

  • . The expansion operator is exactly the same as the slice() method in that it is a deep copy when the copied object is a one-dimensional array or one layer of objects, and a shallow copy when the copy is more than one layer
  • Changes in the value of the original object at the first layer after the copy do not affect, but changes in the value of the original object at the upper layer will affect.
// Deep copy beyond the first layer is shallow copy
let b = [1.2.3.6.4.5];
let a = [b];
let c = [...a];
a[0] = ['xxx'.5.6.4.8.8];
b[0] = 100;
console.log(c);
// [[100, 2, 3, 6, 4, 5]]
Copy the code
// Objects still follow the first deep copy and beyond the first shallow copy
let obj= {
    name:'cometang'.ah: ['photography'.'swimming'.'home'].do: {a:'Write code'.b:'Still write code.'}};letobj1 = {... obj}; obj.name ='cometang11111';
obj.ah[0] ='climbing the mountain';
obj.do.a = 'Go to the movies';
console.log(obj1);
// { name: 'cometang',
// ah: [' climb ', 'swim ',' stay '],
// do: {a: 'watch a movie ', b:' write code '}}
Copy the code

Deep copy implementation

function deepClone(obj){
    // If it is not object, it can be returned directly
    if(typeofobj ! = ='object') return obj;
    // Return null if null
    if(obj == null) return null;
    // Return a date if it is a date
    if(obj instanceof Date) return new Date(obj);
    Return a regular expression if it is a regular expression
    if( obj instanceof RegExp) return new RegExp(obj);
    // Create a new instance of the same class, preserving the class inheritance relationship
    let o = new obj.constructor();  
    // Loop the value
    for(let key in obj){
        // If the current value is an object, perform the recursive judgment, if it is not an object, directly assign the value
        o[key] = typeof obj[key] === 'object'? deepClone(obj[key]):obj[key]; }return o;
}
let arr = deepClone([12.5.6.8.9.2, {name:'1111'}]);
console.log(arr);
//[ 12, 5, 6, 8, 9, 2, { name: '1111' } ]
Copy the code