L1-Object Methods Flashcards

1
Q

Object methods: keys, values, entries, assign, freeze

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

Object methods: keys, values, entries, assign, freeze

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

Assign entries from the second object argument to the end of the first object argument.
+
Mutates the first object argument
+
Returns the mutated first argument object.

SIMPLE LANGUAGE
- a method for combining and/or copying objects

A

Object.assign(myObj1, myObj2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What is logged to console from:
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);

console. log(target);
console. log(source);
console. log(returnedTarget);

A
// expected output: Object { a: 1, b: 4, c: 5 }
// expected output: Object  { b: 4, c: 5 }
// expected output: Object { a: 1, b: 4, c: 5 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What is logged to console from:
let myObject1 = {one: "1"};
let myObject2 = {2: "2"};
Object.assign(myObject1, myObject2)['one'] = 'seven';
console.log(myObject1);

Why

A

// expected output: { ‘2’: ‘2’, one: ‘seven’ }

Object.assign() returns the mutated first argument object.

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

What is logged to console.

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);

console. log(target);
console. log(source);
console. log(returnedTarget);

A

// expected output: Object { a: 1, b: 4, c: 5 }

// expected output: Object { b: 4, c: 5 }

// expected output: Object { a: 1, b: 4, c: 5 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5, d: {e: 6}};
const returnedTarget = Object.assign(target, source);

What is returned from the following

target. b = 444;
console. log(target);
console. log(source);
console. log(returnedTarget);

source. b = 444;
console. log(target);
console. log(source);
console. log(returnedTarget);

source. d.e = 666;
console. log(target);
console. log(source);
console. log(returnedTarget);

A

Both target and returned target are changed. But not the source.

Only the source is changed.

The nested object of the source, target and returned target are all changed (it’s the same object being referenced)

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

What happens when a source has the same key name as the target?

let user = { name: “John” };

Object.assign(user, { name: “Pete” });

alert(user.name);

A

The target key gets overridden

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

Does Object.assign({}, oldObject) do a deep or shallow copy?

A

Shallow

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

What does
Object.assign()
do?
and return?

A

Copies all enumerable own properties from one or more source objects to a target object
Object.assign(target, …sources)

Returns the target object argument.

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

Convert a JSON file into an object

A

let apple = require(‘./test.json’);

let newApple = JSON.parse(JSON.stringify(apple));

console.log(newApple.apple);

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

Stop things happening in array/object argument (shallow)

A

Object.freeze(myObjectorArray)

Only freezes 1 object at a time

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

What does
Object.freeze()
do?
and return?

A

Freezes an object. A frozen object can no longer be changed; freezing an object prevents:

new properties from being added to it
existing properties from being removed
prevents changing the enumerability
configurability or writability of existing properties
and prevents the values of existing properties from being changed.
In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in.
Values that are objects can still be modified, unless they are also frozen.

Returns the object.

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

Which of these throw an error?

let obj = Object.freeze({ a: ‘foo’ });
let arr = Object.freeze([‘a’, ‘b’, ‘c’]);

obj[‘b’] = ‘bar’;

obj.c = ‘ccc’;

arr[3] = ‘x’;

arr.push(‘d’);

A

Trying to use a method to mutate a frozen object raises an exception. However, if we try to use assignment, the assignment fails silently. Another one of those JavaScript quirks!

let obj = Object.freeze({ a: ‘foo’ });
let arr = Object.freeze([‘a’, ‘b’, ‘c’]);

obj[‘b’] = ‘bar’;
obj; // => { a: ‘foo’ }

obj.c = ‘ccc’;
obj; // => { a: ‘foo’ }

arr[3] = ‘x’;
arr; // => [ ‘a’, ‘b’, ‘c’ ]

arr.push(‘d’); // => TypeError: Cannot add property 3, object is not extensible

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

What does this do?
let target = [1, 2];
let source = [3, 4];

Object.assign(target, source);

console.log(target);

A

[3, 4]

Because the target and source have the same keynames!

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