This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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] | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source.number = 10; | |
console.log('source: ', source,'target: ', target); // output presented below |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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] | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let source = 1, | |
target = source; | |
console.log('source: ', source,'target: ', target); | |
source = 10; | |
console.log('source: ', source,'target: ', target); |