Objects in JavaScript Flashcards

1
Q

What are objects?

A

Objects are a complex data type that allow you to bring together common properties and behaviors into a single entity. It’s a great way to organize code that belongs together and helps you to avoid global variables.

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

What do objects hold?

A

Objects hold key and value pairs. The key is the name or word for the value. For example:

const ledZeppelin = {
  singer: 'Robert Plant',
  guitar: 'Jimmy Page',
  bass: 'John Paul Jones',
  drums: 'John Bonham'
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you separate each key/value in an object literal?

A

With a comma. For example:

const ledZeppelin = {
  singer: 'Robert Plant',
  guitar: 'Jimmy Page',
  bass: 'John Paul Jones',
  drums: 'John Bonham'
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When would you use quotations in a key within the object?

A

When you need a space or period in the key. For example:

const ledZeppelin2 = {
‘lead singer’: ‘Robert Plant’,
‘lead guitar’: ‘Jimmy Page’,
}

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

Can a key in an object be used more than once?

A

No, each key must be unique in the object.

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

What types of values can a key have?

A

Any valid JavaScript data type, including numbers, strings, booleans, other objects, null, or functions.

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

What is a method?

A

When an object has a value that is a function, the key/value pair is known as a method.

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

How do you get values or run object methods?

A

Using the dot notation or bracket notation after the key. For example:

ledZeppelin.singer

or

ledZeppelin[‘lead singer’]

Use the bracket when you need to get a key with spaces or periods.

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

How do you add new keys to an object?

A

Enter the name of the object followed by a dot or bracket notation and the new key. For example:

const myFamily = {
  lastName: 'Doe',
  mom: 'Cynthia',
  dad: 'Paul',
};

myFamily.sister = ‘Lucinda’;
myFamily[‘brother’] = ‘Merle’;
myFamily.sayHi = function() {
console.log(Hello from the ${myFamily.lastName}s);
}
myFamily.sayHi() // => Hello from the Does

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

How do you update values in a key?

A

Just like how you add a new key/value pair.

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

How do you delete a key/value pair in an object?

A

Use the delete command. For example:

const foo = {
  bar: true
};
delete foo.bar;
console.log(foo.bar); // => undefined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is self reference?

A

When you repeat the variable name of the object inside a method within the object. For example:

const myFamily = {
  lastName: 'Doe',
  mom: 'Cynthia',
  dad: 'Paul',
  sayHi: function() {
    console.log(`Hello from the ${myFamily.lastName}s`);
  }
};

myFamily.sayHi() // => Hello from the Does

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

How do you self reference with the keyword ‘this’?

A

Replace the variable name in the method with the word ‘this’. For example:

const myFamily = {
  lastName: 'Doe',
  mom: 'Cynthia',
  dad: 'Paul',
  sayHi: function() {
    console.log(`Hello from the ${this.lastName}s`);
  }
};

myFamily.sayHi() // => Hello from the Does

‘this’ refers to the object itself and gives you access to other properties and methods on the object.

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

Why should you be careful when writing functions that take objects as an argument?

A

Because when you modify the variable’s value within the function, you may end up changing the value for the same variable on the global scope, creating a side effect.

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

How do you iterate through key/value pairs in an object?

A

Using the object.keys method. Pass an object to Object.keys, and you get back the keys of that object as an array. For example:

const pageViewCounts = {
  homePage: 399,
  aboutPage: 400,
  termsOfService: 22,
};

console.log(Object.keys(pageViewCounts));

Object.keys(pageViewCounts).forEach(function(key) {
console.log( the ${key} page has ${pageViewCounts[key]} views.);
});

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

What is a factory function?

A

A factory function generates individual instances of the idea of a model. Inside the factory function, we indicate the properties and default values that all instances of the model will have.