Getting inconsistent result in Module 2 part 6 video | JavaScript and Node.js Forum
M
Monal Posted on 20/09/2019

Hi Instructor,

So when listening to "Advance concept of Cloning and Merging" part of the Module2 part 6 vidoe... when I did the example of clone, I am getting ReferenceError, but in the lecture vidoe it says the output is "ghi" also in the lecture video it shows "undefine" see attached screen shot.

Although in the for in loop it should be ... clone[key30] = user30[key30]; instead of clone[key30] = user30[key];

When hearing the video it say the output is "ghi" when I tried to do same example I get ReferenceError please see the attached screen shot.

This video is bit confusing way the material is presented.

Please correct me if I am mistaken 

Thanks,

Monal


M
Monal Replied on 20/09/2019

Here is the screen shot out put after adding insided the "for in" loop with clone[key30] = user30[key30];

...

Monal

Responsive image

Y
Yogesh Chawla Replied on 20/09/2019

Hi Monal,

This shouldn't be the case. Anyhow I have re-recorded the entire module. You can refer this new recording to understand "Copy By Reference. Cloning, Merging and Object.assign method". Just check that once.

And you can also refer this code to understand it better:

// Copy by Reference
let a = {};
let b = a;

console.log(a==b);
console.log(a===b);

let a1 = {};
let b2 = {};

console.log(a1==b2);
console.log(a1===b2);

let a2 = 5;


const user = {
name : "abc"
}

user.age = 28;

console.log(user.name);
console.log(user.age);

//user = {
// name : "xyz"
//}

let user2 = {
name: "abc",
age: 30
};

let clone = {};

for(let key in user2){
clone[key] = user2[key];
}

clone.name = "pqr";

console.log(clone.name);
console.log(user2.name);

// Object.assign(dest, [src1, src2, src3]);

let user3 = {name: "mno"};

let permission1 = {canView : true};
let permission2 = {canEdit : true};

Object.assign(user3, permission1, permission2);

let user4 = {
name: "abc",
age: 30
};

let clone2 = Object.assign({}, user4);

console.log(clone2.name);

let user5 = {
name: "abc",
sizes: {
height: 180,
width: 50
}
};

console.log(user5.sizes.height);

let clone3 = Object.assign({}, user5);

console.log(user5.sizes == clone3.sizes);

user5.sizes.width++;
console.log(user5.sizes.width);
console.log(clone3.sizes.width);

// deep cloning
//lodash _.cloneDeep(obj); 
 
Regards


M
Monal Replied on 20/09/2019

Thank you Instructor for clarification.

... you can close this query.

-Monal