Objects Flashcards

1
Q

What are objects in JavaScript?

A

An object is data that contains key:value pairs

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

How do we create objects in JavaScript

A

We create objects by containing the key:value pairs within curly braces. Multiple K:V need to be separated by commas

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

How can we conceptualize the relationship between key and value in an object

A

Each key references a value

Much like a word and its definition in the dictionary

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

What values can we put in an object?

A

Objects can contain any valid data structure - Primitives and Objects

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

What is another name for keys in an JavaScript object?

A

Keys are also known as

Properties

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

How can we get the value of an object property (key)?

A

We can use dot (.) operator (dot notation)

or

bracket [] notation

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

Give the syntax for using dot notation to get the value of an object property.

A

objectName.propertyName

aka

objectName.keyName

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

Give the syntax for using bracket notation to get the value of an object’s property

A

objectName[‘propertyName’]

note that the propertyName is a string

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

When do you need to use bracket notation to access the value of an object’s properties?

A
  1. When you access the value of a property with a variable.
  2. When the property name is an invalid identifier
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we set the value of a property (key) in an object?

A

We can use either dot notation (preferred) or bracket notation

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

Give the syntax for setting an object property value.

A

objectName.propertyName = value

we can use bracket notation too

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

How do we delete an object key:value pair?

A

delete objectName.propertyName

we can use bracket notation too

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

If two objects hold the same information will the equate to equal using comparison opperators?

A

No, objects are compared by reference, not by content.

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

When will two variables that point to objects be equal using equality opperators?

A

Only when variables point to the same object, or one variable points to a variable pointing to an object, will they be equal.

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

When do keys (or identifiers) need quotation marks in an object literal?

A

Then the key or identifier contains special characters (like a space).

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

How do we create a method in an object literal?

A

Two ways:

  1. Set the method name with the key, and the value as an anonymous function after the colon
  2. Set the method name with the key, and create the function without the colon or function name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Give the syntax for the ES6 method creation in an object.

A

const objectName = {

methodName () { method code block } }

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

What does it mean that Objects are passed by reference

A

It means that when objects are assigned to variables, the variables hold a reference to the object’s location in memory, not that actual values. So, if we mutate a object by passing it to a function, the original object is mutated, regardless of the variable type.

19
Q

Can we reassign objects that are passed as a variable into a function?

A

No. Because objects are passed by reference, when we pass in a object variable to a function, the function variable references the location of data, but not the original variable. Any reassignment occurs only within the function body and not outside

20
Q

How can we iterate through objects?

A

The for … in loop with execute a given block of code for each property in an object

21
Q

Are key:value pairs in objects ordered?

A

No, key:value pairs are not ordered in objects.

22
Q

Do the methods of an object have direct access to other properties of that object.

A

Not directly, other properties are not contained within their scope.

You need to use the “this” keyword, which refers back to the calling object

23
Q

How do we use the keyword this in object methods?

A

We use the keyword this in object methods to gain access to the object properties outside the scope of the method’s code block. This will refer to the calling object, and we use dot-notation to select the property value we need.

24
Q

Will the keyword this work with methods written as arrow functions?

A

No, not without help. Arrow functions inherently bind their keyword this to the global object.

25
Q

Does JavaScript have built in privacy for objects?

A

No

26
Q

What is a conventional way of indicating that a property of an object is private in JavaScript?

A

We and prepend an underline “_” to the property name in an object to indicate privacy.

27
Q

What is the basic use-case for a getter method in JavaScript?

A

A getter method is basically used to get and return the value of an internal property of an object.

28
Q

What is the syntax of a getter method in JavaScript?

A

object {

get fullName() {

codeBlock

}}

29
Q

How do we call or invoke a getter method in JavaScript?

A

We invoke a getter method much like we would access a regular property value of an object.

object.getterName

We do not use the get keyword or add ( ) to the call.

30
Q

What are four advantages to using getter methods on objects?

A
  1. We can use the keyword this to access object properties
  2. We can manipulate or formate properties within the method
  3. We can use conditionals to return different values
  4. The code is easier to read and clearer
31
Q

What should we keep in mind when naming properties and getter methods?

A

Getter methods and object properties may not share names.

32
Q

What do setters do in the context of JavaScript objects?

A

Setters reassign the value of an object property

33
Q

Describe the syntax of a setter method in a JavaScript object

A

objectName {

set age (newAge){

if(typeof newAge === ‘number’){

this._age = newAge

} else {

return ‘age must be a number

}}}

34
Q

How do we invoke a setter method?

A

objectName.setterName = value

We do not use ( ) when invoking the setter

35
Q

What is a factory function?

A

A factory function is a function used to create objects from a basic design or schema.

Instead of creating multiple object literals, we can use the factory function to create objects

36
Q

What is the syntax of a factory function.

A

const factoryName = (property1, property2, property3) => {

return {

property1: property1,
property2: property2,
property3: property3,

aMethod() {

methodCode}

}

}

37
Q

What is ‘property value shorthand’ in relation to JavaScript objects?

A

Property Value Shorthand is a type of destructuring available in ES6.

38
Q

How do we use property value shorthand to assign values to properties in JavaScript objects?

A

When the property and value share their name (as in factory functions) we can list only the property name and omit the value (since it is the same).

39
Q

Convert this factory function into property value shorthand:

function robotFactory(model, mobile){

return {

model: model,
mobile: mobile,

beep() {

console.log(‘Beep Boop’);

}

A

function robotFactory(model, mobile){

return {

model,

mobile,

beep() {

console.log(‘Beep Boop’);

}

40
Q

What is a ‘destructured assignment” in relation to JavaScript objects?

A

A destructure assignment is a shorthand method to assign property values to a variable.

41
Q

Give the syntax of a destructured assignment.

A

const {propertyName } = object

const will now equal propertyValue of propertyName

We still refer to the const by the propertyName

42
Q

What are Object class methods?

A

Object class methods are methods “built-in” to the Object prototype that we can call on the Object data-type.

43
Q

What are two or three common Object Class Methods that we might use?

A

Object.entries(objectName) = returns an array of key:value pairs

Object.keys(objectName) = returns an array of properties

Object.values(objectName) = returns an array of values of an objects properties

44
Q

What is unusual about the invocation of a setter method?

A

When using a setter method we do not include the arguments in parenthesis - instead, we set the objectName.setterMethod = arguments