Skip to content

Instantly share code, notes, and snippets.

@wo17ek
wo17ek / shallowCopyImplementation.js
Last active January 27, 2020 14:32
Implementation of the shallow copy.
function shallowCopy(target, ...sources) {
if(arguments.length === 0) {
return null;
}
else if(arguments.length === 1) {
if(target !== null && target !== undefined) {
sources.unshift(target);
target = {};
}
else {
@wo17ek
wo17ek / manuallyDeepCopiedObject.js
Created January 21, 2020 11:06
Manually deep copied an object.
/*
Copy relies on assigning each source object property into the target with corresponding names.
*/
const source = {
number: 1,
object: {
string: "I like cookies",
array: [1, 2, 3]
}
};
@wo17ek
wo17ek / manuallyMutateSourceObjectProperties.js
Created January 20, 2020 15:46
Manually mutate the source object properties.
source.number = 10;
source.object.string = "I like them too!";
source.object.array = [4, 5, 6];
console.log('source: ', source, 'target: ', target); // output presented below
@wo17ek
wo17ek / manuallyMutateLowerSourceObjectProperties.js
Created January 17, 2020 15:39
Manually mutate the lower-level source object properties.
source.object.string = "I like them too!";
source.object.array = [4, 5, 6];
console.log('source.object === target.object: ', source.object === target.object) // true,
console.log('source: ', source, 'target: ', target); // output presented below
@wo17ek
wo17ek / assignAndReassignNonPrimitiveVariableAndMutated.js
Last active January 17, 2020 08:14
Assign and later reassign one of the non-primitive variables earlier storing the same reference, and see what happens when mutated.
/*
PART 1: assign a non-primitive variable into another.
*/
let source = {
number: 1,
object1: {
string: "I like cookies."
},
object2: {
string: "You dared to eat my cookies!"
@wo17ek
wo17ek / mutateProperty.js
Last active January 15, 2020 17:06
Mutate the number property.
source.number = 10;
console.log('source: ', source,'target: ', target); // output presented below
@wo17ek
wo17ek / manuallyMutateHighestSourceObjectProperties.js
Last active January 17, 2020 15:40
Manually mutate the highest-level source object properties.
source.number = 10;
source.object = {
string: "Where did my cookies go?"
}
console.log('source.object === target.object: ', source.object === target.object) // true, so both store the same reference
console.log('source: ', source, 'target: ', target); // output presented below
@wo17ek
wo17ek / manuallyShallowCopiedObject.js
Last active January 20, 2020 16:02
Manually shallow copied an object.
/*
Copy relies on assigning each source object property into the target with corresponding names.
*/
const source = {
number: 1,
object: {
string: "I like cookies",
array: [1, 2, 3]
}
};
@wo17ek
wo17ek / assignNonPrimitiveVariable.js
Last active January 17, 2020 08:28
Assign the non-primitive variable into another.
const source = {
number: 1,
object: {
string: "I like cookies"
}
};
const target = source;
console.log(source === target) // true, so they both store reference to the same place.
console.log('source: ', source,'target: ', target); // output presented below
@wo17ek
wo17ek / assignPrimitiveVariable.js
Last active January 27, 2020 13:28
Assign a primitive variable into another.
let source = 1,
target = source;
console.log('source: ', source,'target: ', target);
source = 10;
console.log('source: ', source,'target: ', target);