What types of variables do js have

  • Basic types: Number, string, null(special), undefined, Boolean, Symbol, ES10 BigInt(arbitrary precision integer)
  • Reference types: Array, Function, data, RegExp

1.1 stack memory

It is mainly used to store Pointers to basic types and object variables, giving people the feeling that it is like a linear arrangement of space, each small unit size is basically equal, stored variables are generally known size or known upper limit range, is a simple storage

Stack memory automatically allocates a relatively fixed size of memory space and is automatically released by the system

Data is stored and used in the stack memory in a way similar to the stack data structure in the data structure, which follows the principle of first in, then out.

The queue is first-in, first-out

1.2 heap memory

It is used to store reference types such as object. The size of the data stored for the object type is unknown in this respect (which is probably why null is stored in stack memory as an object variable).

Heap memory is dynamically allocated memory, which varies in size and is not automatically freed

Shallow copy and deep copy

The difference between deep copy and shallow copy

The way to distinguish between A deep copy and A shallow copy, to put it simply, is to assume that B copies A, and when you modify A, see if B changes. If B also changes, it’s A shallow copy. If B doesn’t change, it’s A deep copy.

Shallow copy

 var obj = { a: 1, arr: [2, 3] };
  var shallowObj = Object.assign({}, obj);
  shallowObj.arr[1] = 0;
  shallowObj.a = 'yanbo';
  console.log(obj, 'obj');
  console.log(shallowObj, 'shallowObj');
Copy the code

Deep copy

Deep copy / / / / get type function getType (obj) {var toString = Object. The prototype. ToString. var map = { '[object Boolean]': 'boolean', '[object Number]': 'number', '[object String]': 'string', '[object Null]': 'null', '[object Undefined]': 'undefined', '[object Array]': 'array', '[object Function]': 'function', '[object RegExp]': 'regExp', '[object Date]': 'date', '[object Object]': 'object'} if (obj instanceof Element) {// toString returns constructor 'Element' for different tags; } return map[toString.call(obj)]; } function deepClone(data) { var type = getType(data) var obj; if (type === 'array') { obj = [] for (var i = 0; i < data.length; i++) { obj.push(deepClone(data[i])) } } else if (type === 'object') { obj = {} for (var key in data) { obj[key] = deepClone(data[key]); } } else { obj = data; } return obj; } var oldObj = { a: 1, b: 2 } const newObj = deepClone(oldObj); const newObj1 = newObj.c = 4; console.log(oldObj, newObj) let oldData = [1, 2, 3, 4, 4]; const newData = deepClone(oldData); const newData1 = newData.push({ a: 4, b: 7 }); console.log(newData, '--->newData', oldData, '---->oldData');Copy the code

The second kind of deep copy

Json.parse (json.stringify (obj)) is usually used for deep copy. The process is to serialize the JS object (JSON string) using json.stringify, and then deserialize (restore) the JS object using json.parse. Serialization is used for storage (the object itself only stores an address map, if the power is turned off, the object will no longer exist, Therefore, the Content of the object needs to be converted into a string and saved on disk (for example, if the requested content-type is Application/X-www-form-urlencoded, Then the front end needs to use qs.stringify(data) to serialize the parameters and pass them to the back end, otherwise the back end cannot receive them; Ps: the content-type for application/json; Charset =UTF-8 or multipart/form-data is not required).

If obj contains a time object, then the result of json. stringify followed by json. parse will be a string of time. Rather than a time object;

  var a = {
    name: 'aaa',
    date: [new Date(1536627600000), new Date(1540047600000)]
  }
  let b;
  b = JSON.parse(JSON.stringify(a))
  console.log(a);
  console.log(b);
Copy the code

If obj contains RegExp and Error objects, then the serialized result will be null;

  const test = {
    name: 'a',
    date: new RegExp('\\w+'),
  };
  // debugger
  const copyed = JSON.parse(JSON.stringify(test));
  test.name = 'test'
  console.log(test);
  console.log(copyed);
  console.error('ddd', test, copyed)
Copy the code

3, if obj contains function, undefined, then the serialization result will lose the function or undefined;

  const test = {
    name: 'aaa',
    date: function hello() {
      console.log('ffff');
    }
  }
  const copy = JSON.parse(JSON.stringify(test));
  test.name = 'js';
  console.log(test)
  console.log(copy);
Copy the code

If NaN, Infinity and -infinity are in obj, the serialized result will be null