Skip to content

Commit 41ad7ea

Browse files
committed
Merge branch 'master' of github.com:travisjupp/Javascript
2 parents 502571e + 8eb92fd commit 41ad7ea

2 files changed

Lines changed: 129 additions & 0 deletions

File tree

shallowVSDeepCopy.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const originalObject = {
2+
somePrimitive: 3,
3+
someReference: {
4+
someNestedPrim: 7,
5+
someNestedRef: ["a", "b"]
6+
},
7+
anotherPrimitive: "hello"
8+
};
9+
10+
// Create a shallow copy using the spread syntax
11+
const shallowCopy = { ...originalObject };
12+
13+
console.log("--- Initial State ---");
14+
console.log("Original Object:", JSON.stringify(originalObject));
15+
console.log("Shallow Copy:", JSON.stringify(shallowCopy));
16+
17+
console.log("\n--- Modifying the shallow copy ---");
18+
19+
// 1. Changing a root-level primitive property in the shallow copy
20+
shallowCopy.somePrimitive = 10;
21+
console.log("shallowCopy.somePrimitive changed to:", shallowCopy.somePrimitive);
22+
console.log("Original Object's somePrimitive:", originalObject.somePrimitive); // Original unaffected
23+
24+
// 2. Changing a root-level primitive property that was added to illustrate the point
25+
shallowCopy.anotherPrimitive = "world";
26+
console.log("shallowCopy.anotherPrimitive changed to:", shallowCopy.anotherPrimitive);
27+
console.log("Original Object's anotherPrimitive:", originalObject.anotherPrimitive); // Original unaffected
28+
29+
30+
// 3. Modifying a property *within* a nested object in the shallow copy
31+
shallowCopy.someReference.someNestedPrim = 15;
32+
console.log("shallowCopy.someReference.someNestedPrim changed to:", shallowCopy.someReference.someNestedPrim);
33+
console.log("Original Object's someReference.someNestedPrim:", originalObject.someReference.someNestedPrim); // Original AFFECTED
34+
35+
36+
// 4. Modifying a nested array in the shallow copy
37+
shallowCopy.someReference.someNestedRef.push("c");
38+
console.log("shallowCopy.someReference.someNestedRef after push:", JSON.stringify(shallowCopy.someReference.someNestedRef));
39+
console.log("Original Object's someReference.someNestedRef after push:", JSON.stringify(originalObject.someReference.someNestedRef)); // Original AFFECTED
40+
41+
// 5. Reassigning a root-level reference property in the shallow copy
42+
shallowCopy.someReference = { newProperty: true };
43+
console.log("shallowCopy.someReference reassigned to:", JSON.stringify(shallowCopy.someReference));
44+
console.log("Original Object's someReference:", JSON.stringify(originalObject.someReference)); // Original still points to the old object

shallowVSDeepCopy.test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// import {describe, it} from 'node:test';
2+
import assert from 'node:assert/strict';
3+
import {jest} from '@jest/globals';
4+
import util from 'node:util';
5+
import {style} from './styles.js';
6+
util.inspect.defaultOptions.depth = null; // show full objects
7+
// util.inspect.defaultOptions.depth = 0; // show truncated objects
8+
// util.inspect.defaultOptions.compact = true; // dont break objects to new lines
9+
// util.inspect.defaultOptions.compact = false; // break objects to new lines
10+
11+
// suppress jests tracing console logs
12+
import console from 'console';
13+
const jestConsole = console;
14+
15+
beforeEach(() => {
16+
global.console = console;
17+
console.log(style.color(255,0,255),'▷',style.reset,style.color(39),expect.getState().currentTestName,style.reset,'\n'); });
18+
19+
afterEach(() => {
20+
global.console = jestConsole;
21+
console.log(style.color(99), style.hr.double, style.reset);
22+
});
23+
24+
describe('shallowVSDeepCopy', () => {
25+
it('Shallow-Copy VS Deep-Copy concept in JS', () => {
26+
27+
const originalObject = {
28+
somePrimitive: 3,
29+
someReference: {
30+
someNestedPrim: 7,
31+
someNestedRef: ["a", "b"]
32+
},
33+
anotherPrimitive: "hello"
34+
};
35+
36+
// Create a shallow copy using the spread syntax
37+
const shallowCopy = { ...originalObject };
38+
39+
console.log(style.bold,style.underline,style.blue,"--- Initial State ---",style.reset);
40+
console.log("Original Object:", originalObject);
41+
console.log("Shallow Copy:", shallowCopy, "\n");
42+
43+
console.log(style.bold,style.underline,style.blue,"--- Modifying the shallow copy ---",style.reset);
44+
45+
// 1. Changing a root-level primitive property in the shallow copy
46+
console.log(style.red,"1. Changing a root-level primitive property in the shallow copy", style.reset);
47+
48+
shallowCopy.somePrimitive = 10;
49+
console.log("shallowCopy.somePrimitive changed to:", shallowCopy.somePrimitive);
50+
console.log("Original Object's somePrimitive:", originalObject.somePrimitive, style.italic, style.magenta, "// Original unaffected\n", style.reset); // Original unaffected
51+
52+
// 2. Changing a root-level primitive property that was added to illustrate the point
53+
console.log(style.red,"2. Changing a root-level primitive property that was added to illustrate the point", style.reset);
54+
55+
shallowCopy.anotherPrimitive = "world";
56+
console.log("shallowCopy.anotherPrimitive changed to:", shallowCopy.anotherPrimitive);
57+
console.log("Original Object's anotherPrimitive:", originalObject.anotherPrimitive, style.italic, style.magenta, "// Original unaffected\n", style.reset); // Original unaffected
58+
59+
60+
// 3. Modifying a property *within* a nested object in the shallow copy
61+
console.log(style.red,"3. Modifying a property *within* a nested object in the shallow copy", style.reset);
62+
63+
shallowCopy.someReference.someNestedPrim = 15;
64+
console.log("shallowCopy.someReference.someNestedPrim changed to:", shallowCopy.someReference.someNestedPrim);
65+
console.log("Original Object's someReference.someNestedPrim:", originalObject.someReference.someNestedPrim, style.italic, style.magenta, "// Original AFFECTED\n", style.reset); // Original AFFECTED
66+
67+
68+
// 4. Modifying a nested array in the shallow copy
69+
console.log(style.red,"4. Modifying a nested array in the shallow copy", style.reset);
70+
71+
shallowCopy.someReference.someNestedRef.push("c");
72+
console.log("shallowCopy.someReference.someNestedRef after push:", shallowCopy.someReference.someNestedRef);
73+
console.log("Original Object's someReference.someNestedRef after push:", originalObject.someReference.someNestedRef, style.italic, style.magenta, "// Original AFFECTED\n", style.reset); // Original AFFECTED
74+
75+
// 5. Reassigning a root-level reference property in the shallow copy
76+
console.log(style.red,"5. Reassigning a root-level reference property in the shallow copy", style.reset);
77+
78+
shallowCopy.someReference = { newProperty: true };
79+
console.log("shallowCopy.someReference reassigned to:", shallowCopy.someReference);
80+
console.log("Original Object's someReference:", originalObject.someReference, style.italic, style.magenta, "// Original still points to the old object", style.reset); // Original still points to the old object
81+
});
82+
83+
});
84+
85+

0 commit comments

Comments
 (0)