Skip to content

Instantly share code, notes, and snippets.

@wo17ek
wo17ek / Attempt to copy the property whose accessible descriptor in the target is already defined within its prototype-chain.js
Last active March 20, 2020 11:43
Attempt to copy the property whose accessible descriptor in the target is already defined within its prototype-chain.
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);
}
});
@wo17ek
wo17ek / Attempt to copy the property whose accessible descriptor in the target is already defined.js
Last active March 20, 2020 11:43
Attempt to copy the property whose accessible descriptor in the target is already defined.
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
@wo17ek
wo17ek / Attempt to copy the property whose corresponding one in the target prototype-chain already exist and has the writable flag set on false.js
Last active March 20, 2020 11:44
Attempt to copy the property whose corresponding one in the target prototype-chain already exist and has the writable flag set on false.
const target = {};
const targetParent = {};
Object.defineProperty(targetParent, 'p', {
value: 1,
writable: false // is false by default anyway
});
const source = {
@wo17ek
wo17ek / Attempt to copy the property whose corresponding one in the target already exist and has the writable flag set on false.js
Last active March 20, 2020 11:44
Attempt to copy the property whose corresponding one in the target already exist and has the writable flag set on false.
const target = {};
Object.defineProperty(target, 'p', {
value: 1,
writable: false // is false by default anyway
});
const source = {
p: 2
};
@wo17ek
wo17ek / Copying the same property from several objects.js
Last active March 20, 2020 11:44
Copying the same property from several objects.
const source_1 = {
someProperty: 'source_1'
};
const source_2 = {
someProperty: 'source_2'
};
const source_3 = {
someProperty: 'source_3'
};
@wo17ek
wo17ek / Manually deep copying the source object.js
Created March 20, 2020 11:07
Manually deep copying the source object.
// 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
@wo17ek
wo17ek / Manually mutate the source object properties.js
Created March 20, 2020 11:07
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 / Manually shallow copying the source object.js
Created March 20, 2020 11:07
Manually shallow copying the source 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]
}
};
const target = {}; // reference points at an empty structure where we will add properties from our source object
@wo17ek
wo17ek / Assign and later reassign one of the non-primitive variables earlier storing the same reference, and see what happens when mutated.js
Created March 20, 2020 11:03
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 to another.
let source = {
number: 1,
object1: {
string: "I like cookies."
},
object2: {
string: "You dared to eat my cookies!"
@wo17ek
wo17ek / Assign the primitive variable into another.js
Created March 20, 2020 11:02
Assign the primitive variable into another.
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