JS achieve deep copy, shallow copy method

In JS, functions and objects are shallow copy (reference address); other, e.g. Boolean, digital and other basic data types are deep copy (reference value).

Deep copy

  1. JSON.parse (JSON.stringify (src)): This method has limitations if the attribute value is an instance of a class or a function of time, can not copy the correct
  2. With third-party libraries: JQ's extend (true, result, src1, src2 [, src3]), lodash of cloneDeep (src)
  3. By means of HTML5 MessageChannel: this approach has limitations, when the attribute value is a function of time, will be given
  4. Manually make a "perfect" deep copy function: https://godbmw.com/passages/2019-03-18-interview-js-code/

Shallow copy

  1. ES6 the Object.assign ()
  2. The operator deconstruction ... ES7

Guess you like

Origin www.cnblogs.com/zppsakura/p/11387800.html