Restricted Deep Copy Implementations
Explore how to perform deep copies of JavaScript objects using JSON and jQuery extend methods. Understand their limitations, what is lost during cloning, and why refactoring might be a better solution when cloning complex objects. This lesson helps you grasp the nuances of cloning to write more reliable JavaScript code.
We'll cover the following...
JSON methods
There is a very easy implementation for making deep copies of JavaScript objects. Convert the JavaScript object into a JSON string, then convert it back into a JavaScript object.
Restrictions:
-
Object
ohas to be finite; otherwise,JSON.stringifythrows an error. -
The
JSON.stringifyconversion has to be lossless. Therefore, methods not allowed as members of typefunctionare ignored by the JSON stringifier. Theundefinedvalue is not allowed either. Object ...