Object Methods Flashcards

1
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
2
Q

Return object or array keys as array
Return object or array value as array
Return object or array entries as array

A

Object.keys(myObj)
Object.values(myObj)
Object.entries(myObj)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
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
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

How do you create an object method? (During object creation 3 ways, after object creation 2 ways)

A
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');
}
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

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

A

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)

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

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

A

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)

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

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

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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
15
Q

What does new.target return?

What’s it for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is returned
function apple() {
  return new.target
}

console. log(apple())
console. log(new apple);

A

undefined

the apple function code

17
Q

What does:
Object.prototype.valueOf()
do?
Why would you need it?

A
It returns the value.
Useful as a method on objects to return a different value for valueOf or when converting to number.
let user = {
  name: "John",
  money: 1000,
  // for hint="number" or "default"
  valueOf() {
    return this.money;
  }
};
It can also be a function:
function MyNumberType(n) {
  this.number = n;
}

MyNumberType.prototype.valueOf = function() {
return this.number; //or some other complicated calculation
};

const object1 = new MyNumberType(4);

console.log(object1 + 3);
// expected output: 7
18
Q

What does:
Object.fromEntries()
do?
Why would you need it?

A

Returns an object from an array:

let prices = Object.fromEntries([
  ['banana', 1],
  ['orange', 2],
  ['meat', 4]
]);

// now prices = { banana: 1, orange: 2, meat: 4 }

Can be used to transform a Map into an Object

19
Q
Return an object:
{ banana: 1,
orange: 2,
meat: undefined,
}
 from an array:
[
  ['banana', 1],
  ['orange', 2],
  ['meat']
]
A

Object.fromEntries(array)

20
Q

What’s the difference between Object.keys() etc. and myMap.keys() etc?

A

Object.keys returns an array, myMap.keys() returns an iterable.

21
Q

How can you from array methods on objects?

A

Use Object.entries(obj) to get an array of key/value pairs from obj.

Use array methods on that array, e.g. map, to transform these key/value pairs.

Use Object.fromEntries(array) on the resulting array to turn it back into an object.

22
Q

Add a pre existing funciton to an object

  • During object creation
  • after object creation
A
//Assigning a pre-declared function
function methodName() {
}
let anObject = {
  methodName
}
//Assigning a predeclared function
function methodName() {
}
myObject.anotherMethodName = methodName;