How a structured cloning algorithm differs from a deep copy

There is an MDN article that states:

The Structured Cloning Algorithm is a new algorithm defined by the HTML5 specification for serializing complex JavaScript objects. It is more than JSON

So, I believe that this means that it is more capable than cloning in this way:

JSON.parse(JSON.stringify(obj))

suggested this topic . The JSON method has many drawbacks, for example, the lack of support for circular references, the removal of everything that is not supported by the JSON specification, like functions, and the representation of the Date object as strings.

Then I thought that a structured cloning algorithm is a deep copy algorithm implemented by many libraries. However, it is listed on the same page with the words:

If you need a deep copy of an object (that is, a recursive copy of all nested properties, walking along the prototype chain), you should use a different approach. Below is a possible example.

So now I am confused and do not know what a structured algorithm is or how it differs from a deep copy. Any help? I don’t want to read the specification, maybe some libraries have implemented it, so I can read their source code.

+5
source share
2 answers

- JavaScript object. API- , .

, JSON.parse JSON.stringify?

JSON.parse(JSON.stringify(obj)) , , , Map, Set, Date, RegEx ..

, API ( ) .

?

API- ( ) , API-. , API .

apis :

  1. history.pushState
  2. MessageChannel
  3. Notification

API, Surma.

0

, , .

:

" ", - StructuredClone. , .

, .

, .

-1

Source: https://habr.com/ru/post/1660188/


All Articles