What’s the difference between an arrow function and a normal function?

  1. A normal function’s this refers to the object on which it was called. Arrow functions’ this is fixed (and there are no other local variables, such as this, arguments, super), which refer to the object on which the function was defined.
  2. Arrow functions cannot use new as constructors.

What does the new operator do?

  1. An empty object is created

var obj = new Object()

  1. Set prototype chain

obj.proto= Func.prototype

  1. Make Func’s this point to obj and execute the body of the Func function

Func.call(obj)

  1. Determine the return value type of the Func function.

If Func returns a nonreference type or no value, obj is returned; If it is a reference type, the reference type object is returned

What is the difference between call, apply and bind?

Similarity: Both are used to change the this reference of functions

  1. Call and apply are used to change this when calling a function, where call passes function arguments one by one and apply passes function arguments in arrays.
  2. Bind simply binds this to the function, returns a new function that this points to, and does not execute that function immediately. The new function argument returned always has the argument at the time of binding.
function fn(a, b, c) {
    console.log(a, b, c);
}
var fn1 = fn.bind(null, 'Dot');

fn('A', 'B', 'C');            // A B C
fn1('A', 'B', 'C');           // Dot A B
fn1('B', 'C');                // Dot B C
fn.call(null, 'Dot');      // Dot undefined undefined
Copy the code

What is the difference between shallow copy and deep copy?

Js basic data types are stored in stack memory, comparing is the comparison of values; The reference data type is stored in the heap, the comparison is the reference comparison, two independent objects, JS store two Pointers to the heap memory address, copy object A to b, is the object stored in the stack address copy, they still point to the same heap address. So there’s an interaction between the two.

Deep copy and shallow copy are concepts for objects, including arrays.

  1. Shallow copy refers to copying only one layer of an object’s properties and not copying if there are still objects in the property value.

Shallow copy method:

Object.assign()

let obj2 = {… obj1}

Custom shallow copy method:

function shallowCopy (obj) {
    let copyObj = {}
    for (let key in obj) {
        console.log(key, 'key')
        if (obj.hasOwnProperty(key)) {
            copyObj[key] = obj[key]
        }
   }
   return copyObj
}
Copy the code
  1. A deep copy is a copy of an object and all its children

Parse & json. stringify, undefined, function, and symbol values in non-array objects are ignored, and values in arrays are converted to NULL.

Custom a compact version of the deep copy method:

function deepCopy (obj) {
    let result = {}
    Object.keys(obj).forEach(key => {
        if (obj[key] && typeof obj[key] === 'object') {
            result[key] = deepCopy(obj[key])
        } else {
            result[key] = obj[key]
        }
    });
    return result
 }
Copy the code

The last