Object references and copying Flashcards

1
Q

let user = { name: ‘John’ };
let admin = user;
admin.name = ‘Pete’;
alert(user.name);

// returns and why?

A

// ‘Pete’, changes are seen from the “user” reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

let a = {};
let b = a;
alert( a == b ); // returns?

A

true, both variables reference the same object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

let a = {};
let b = a;
alert( a === b );

A

// true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

let a = {};
let b = {};
alert( a == b ); // returns and why?

A

false

And here two independent objects are not equal, even though they look alike (both are empty):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

const user = {
name: “John”
};
user.name = “Pete”;
alert(user.name); //

returns and why

A

Pete

An important side effect of storing objects as references is that an object declared as const can be modified.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

let user = {
name: “John”,
age: 30
};

let clone = {};
// copy all user properties into it for in loop

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

let user = { name: “John” };
Object.assign(user, { name: “Pete” });
alert(user.name); // returns and why?

A

now user = { name: “Pete” }

If the copied property name already exists, it gets overwritten

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

let user = {
name: “John”,
age: 30
};

let clone = // assign user to clone

alert(clone.name); // John
alert(clone.age); // 30

A

Object.assign({}, user);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

let user = {
name: “John”,
sizes: {
height: 182,
width: 50
}
};

alert( ); // 182
how to access 182?

A

user.sizes.height

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

let user = {
name: “John”,
sizes: {
height: 182,
width: 50
}
};

let clone = Object.assign({}, user);

alert( user.sizes === clone.sizes ); // returns and why?

A

// true, same object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

let user = {
name: “John”,
sizes: {
height: 182,
width: 50
}
};

user.sizes.width = 60; // returns

A

user.sizes.width = 60; // change a property from one place

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

let user = {
name: “John”,
sizes: {
height: 182,
width: 50
}
};

let clone = Object.assign({}, user);

alert( user.sizes === clone.sizes ); //

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The call _______clones the object with all nested properties.

A

structuredClone(object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

let user = {
name: “John”,
sizes: {
height: 182,
width: 50
}
};

let clone = structuredClone(user);

alert( user.sizes === clone.sizes );

A

// false, different objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

let user = {};
user.me = user;

let clone = structuredClone(user);
alert(clone.me === clone); //

returns and why?

A

true

// let’s create a circular reference:
// user.me references the user itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

there are cases when structuredClone fails.

For instance, when an object has a________

A

function property:

17
Q

The call ___clones the object with all nested properties.

A

structuredClone(object)