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 target = {}; | |
const targetParent = {}; | |
Object.defineProperty(targetParent, 'p', { | |
set(value) { //take over the default one for the 'p' property | |
console.log("Set from the targetParent", value); | |
} | |
}); |
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 target = {}; | |
Object.defineProperty(target, 'p', { | |
set(value) { //take over the default one for the 'p' property | |
console.log("Set from the target", value); | |
} | |
}); | |
const source = { | |
p: 2 |
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 target = {}; | |
const targetParent = {}; | |
Object.defineProperty(targetParent, 'p', { | |
value: 1, | |
writable: false // is false by default anyway | |
}); | |
const source = { |
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 target = {}; | |
Object.defineProperty(target, 'p', { | |
value: 1, | |
writable: false // is false by default anyway | |
}); | |
const source = { | |
p: 2 | |
}; |
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_1 = { | |
someProperty: 'source_1' | |
}; | |
const source_2 = { | |
someProperty: 'source_2' | |
}; | |
const source_3 = { | |
someProperty: 'source_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
// Copy relies on assigning each source object property to the target with corresponding names. | |
const source = { | |
number: 1, | |
object: { | |
string: "I like cookies", | |
array: [1, 2, 3] | |
} | |
}; | |
const target = {}; // reference points at an empty structure where we will add properties from our source object |
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
// 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] | |
} | |
}; | |
const target = {}; // reference points at an empty structure where we will add properties from our source object |
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 to 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
let source = 1, | |
target = source; | |
console.log('source: ', source, ' target: ', target); // source: 1 target: 1 | |
source = 10; | |
console.log('source: ', source, ' target: ', target); // source: 10 target: 1 |