Chapter 10, Ensuring Purity – Immutability
10.1 Not just date problems: We can see more limitations of jsonCopy() in the following example, but don’t assume there aren’t any more problems:
const agent = {
error: new Error("It's stirred; I ordered it shaken"),
map: new Map([["James", "Bond"]]),
set: new Set([0, 0, 7]),
regex: /007/,
useLicense() {
console.log("Bang! Bang!");
},
};
console.log(jsonCopy(agent));
/*
{ error: {}, map: {}, set: {}, regex: {} }
*/
Four of the properties got transformed into an empty object, and the function was ignored.
10.2 The mote in jsonCopy’s eye… Our deepCopy() function does marginally better; with the same agent object as in the previous question, copying produces the following:
/*
{
error: Error: It's stirred; I ordered it shaken
...