L1-Object Methods Flashcards
Object methods: keys, values, entries, assign, freeze
Object methods: keys, values, entries, assign, freeze
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
Object.assign(myObj1, myObj2)
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);
// 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 }
What is logged to console from: let myObject1 = {one: "1"}; let myObject2 = {2: "2"}; Object.assign(myObject1, myObject2)['one'] = 'seven'; console.log(myObject1);
Why
// expected output: { ‘2’: ‘2’, one: ‘seven’ }
Object.assign() returns the mutated first argument object.
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);
// 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 }
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);
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)
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);
The target key gets overridden
Does Object.assign({}, oldObject) do a deep or shallow copy?
Shallow
What does
Object.assign()
do?
and return?
Copies all enumerable own properties from one or more source objects to a target object
Object.assign(target, …sources)
Returns the target object argument.
Convert a JSON file into an object
let apple = require(‘./test.json’);
let newApple = JSON.parse(JSON.stringify(apple));
console.log(newApple.apple);
Stop things happening in array/object argument (shallow)
Object.freeze(myObjectorArray)
Only freezes 1 object at a time
What does
Object.freeze()
do?
and return?
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.
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’);
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
What does this do?
let target = [1, 2];
let source = [3, 4];
Object.assign(target, source);
console.log(target);
[3, 4]
Because the target and source have the same keynames!