Object Methods Flashcards
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)
Return object or array keys as array
Return object or array value as array
Return object or array entries as array
Object.keys(myObj)
Object.values(myObj)
Object.entries(myObj)
Stop things happening in array/object argument (shallow)
Object.freeze(myObjectorArray)
Only freezes 1 object at a time
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
How do you create an object method? (During object creation 3 ways, after object creation 2 ways)
During Object Creation: //Method declaration let myObject = { methodName: function() { } }
//Method declaration shorthand user = { sayHi() { } }
//Arrow function watermelon: x => { console.log('watermeln'); },
After Object Creation
//Function Expression myObject.methodName = function() { }
//Arrow Function anObject.cherry = () => { console.log('cherry'); }
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.
What does
Object.keys()
do?
and return?
Returns an array of a given object’s own enumerable property names, iterated in the same order that a normal loop would
Object.keys(obj)
What does
Object.values()
do?
and return?
Returns an array of a given object’s own enumerable property values, in the same order as that provided by a for…in loop.
Object.values(obj)
What does
Object.entries()
do?
and return?
Returns an array of a given object’s own enumerable string-keyed property [key, value] pairs. An array with nested arrays. This is the same as iterating with a for…in loop, except that a for…in loop enumerates properties in the prototype chain as well
Object.entries(obj)
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.
What does new.target return?
What’s it for?
If run not apart of a constructor statement, it returns undefined.
If run as a cnstructor it returns the constructor function code
function User() { alert(new.target); }
// without "new": User(); // undefined
// with "new": new User(); // function User { ... }
It lets you detect whether a function or constructor was called using the new operator.