This is the second day of my participation in the August More Text Challenge

What are the data types commonly used in JavaScript

  • Basic data types (5)
    • Number
    • String
    • Boolean
    • Null
    • undefined
    • Symbol (new in ES6)
  • Reference data type
    • Object
  • The difference between the two: storage in memory is different

1.1 What are the Type detection methods? (Four kinds are summarized)

  • Typeof (most commonly used)
    • use
    typeof 'abc'
    Copy the code
    • Returns a String of data names, for example, String, Number…
    • Null -> object
  • Instanceof (used less often)
    • It is mainly used for detection in complex data types.
    • Any complex type is a subtype of Object, so checking for array type returns object.
  • Constructor (constructor attributes)
    • Stereotype inheritance may be overwritten (inaccurate)
  • Object. The prototype. ToString. Call () (use)
    • Return: [object XXX]
    • [Root@tostring] [root@toString] [root@toString] [root@toString] [root@toString] [root@toString] [root@toString

The properties of an object are searched up the prototype chain when used, following the proximity principle, and the function that ends up being used is the most recently found

2. What are the differences between primitive types and object types?

  • Different storage methods

    • Different amounts of data can be stored.
      • Storing a single value
      • Stores an associated set of data
    • They are stored differently in memory
      • Raw values store concrete values in the memory cell and are stored on the stack
      • Object stores the address of a specific value in the memory cell, stored in the heap
  • Results vary when operating

    • There are differences in type detection
    • The result of the assignment operation is different
      • Assignment: Operations performed on a variable’s memory unit
      • Different function parameters
        • Duplicate values
        • Refer to the address

2.2

  • All string methods must operate on return values
  • Array methods, most of which do not require operations based on return values

3. How do I copy objects?

  • Copy: Just as the name implies, copy
    • After a successful copy, the two files are separate and independent. Modifying data in one file does not change the other.
  • Objects are divided into: 1. Objects that store data only, and there is no function in data. 2. Tool objects
  • How to copy an array?
    • Shallow copy: Copies only the attributes of the current object (one layer). The attributes cannot be fully copied.
      • Traversal or built-in methods or extension operators
    • Deep copy: full copy
  • How are objects copied?
    • Deep copy
      • Method 1: Use JSON objects
        / / copy
        JSON.parse(JSON.stringify(obj))
        Copy the code
      • Method 2: Recurse
        // Deep copy: recursive implementation
          const obj = {
              name: 'meimei'.age: 18.friend: {
                  gender: 'woman'}}const obj2 = {};
          // Deep copy function deepClone(source,targe);
          function deepClone(source,target){
              for(let i in source){
                  // Check for data types; object types need to be traversed internally
                  if(typeof source[i] === 'object'&& source[i] ! = =null){
                      target[i] = source[i] instanceof Array ? [] : {};
                      deepClone(source[i],target[i]);
                  }else{
                      // iterate over assignments
                      target[i] = source[i];
                  }
              }
          }
        
          deepClone(obj,obj2);
          // Modify the original object
          obj.name = 'plum';
          obj.friend.gender = 'male';
          console.log(obj,obj2);
        Copy the code

3.1 Deep freeze works in the same way as deep copy

  • object.freeze()