Objects Flashcards

1
Q

Introduction to JavaScript Objects

A

Represent real-world objects in JavaScript
Access object properties
Access object methods
Create object getter and setter methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Change method to ES6:
let person = {
   sayHello: () => {
    return 'Hello, there!';
  }
}
A

sayHello() {
return ‘Hello, there!’;
}

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

‘this’ keyword?

A

methods that operate on the data inside of the same object.

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

‘this’ example

A
const restaurant = { 
  hasDineInSpecial: true,
  openRestaurant() {
    if (this.hasDineInSpecial) {
      return 'Unlock the door, post the special on the board, then flip the open sign.';
    } 
  }
};

console.log(restaurant.openRestaurant());

// "this.hasDineInSpecial" 
// inside the object is the same as accessing 
// "restaurant.hasDineInSpecial" 
// outside the object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Getters and Setters: 3 advantages?

A

You can check if new data is valid before setting a property.
You can perform an action on the data while you are getting or setting a property.
You can control which properties can be set and retrieved.

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

Review: Objects

A

Objects store key-value pairs and let us represent real-world things in JavaScript.
Properties in objects are separated by commas. Key-value pairs are always separated by a colon.
You can add or edit a property within an object with dot notation.
A method is a function in an object.
this helps us with scope inside of object methods. this is a dynamic variable that can change depending on the object that is calling the method.
Getter and setter methods allow you to process data before accessing or setting property values.

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

Which line of code will log the value saved to the _num key in the tempObj object.

let tempObj = {
    _num: 22,
    get num() {
        return this._num;
    }
};
Which line of code will log the value saved to the `_num` key in the `tempObj` object.

console. log(tempObj.’_num’);
console. log(tempObj[num]);
console. log(tempObj.num);
console. log(tempObj.num());

A

console.log(tempObj.num);

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

What data types can you set as values in an object?

What data types can you set as values in an object?

All of the options

Strings

Objects

Arrays

A

All of the options

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

How can we access the seatingCapacity value in the restaurant object?

let restaurant = {
  name: 'Italian Bistro',
  seatingCapacity: 120,
  hasDineInSpecial: true,
  entrees: ['Penne alla Bolognese', 
        'Chicken Cacciatore', 'Linguine pesto']
}
How can we access the `seatingCapacity` value in the `restaurant` object?

restaurant[seatingCapacity]

restaurant{seatingCapacity}

restaurant.seatingCapacity

restaurant#seatingCapacity

A

restaurant.seatingCapacity

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

What is a method?

What is a method?

A method is a function that takes an object as its parameter.

A method is a general term used to describe how to create objects.

A method is a function created inside of an object.

A

A method is a function created inside of an object.

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

Which of the following statements is correct?

Which of the following statements is correct?

Objects store data as key/value pairs.

Objects are containers for variables.

Objects store data at numbered positions.

Objects can only store numbers, strings, and booleans.

A

Objects store data as key/value pairs.

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

How can we add a property to the object below?

let bikes = {
  schwinn: 'blue',
  trek: 'black'
}
How can we add a property to the object below?

bikes = specialized: ‘red’;

bikes.’specialized’ = ‘red’;

let bikes.specialized = ‘red’

bikes[‘specialized’] = ‘red’;

A

bikes[‘specialized’] = ‘red’;

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

Which is the correct syntax for an object?

// 1
let myObject = {
  greeting: 'hello'
};
// 2
let myObject = {
  greeting = 'hello'
};
// 3
let myObject: {
  greeting = 'hello'
};
// 4
let myObject; {
  greeting: 'hello'
};
Which is the correct syntax for an object?
A

1

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

How can we call the method in the code below?

let myObj = {
  sayHello() {
    return 'Hello there!';
  }
}
How can we call the method in the code below?

myObj.sayHello

myObj.sayHello()

myObj().sayHello()

myObj[‘sayHello’]

A

myObj.sayHello()

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

What number is logged to the console in the following example?

let tempObject = {
  num: 2,
  numSquared() {
    num = 3;
    return this.num * this.num;
  }
};

console.log(tempObject.numSquared());
What number is logged to the console in the following example?

6

9

4

2

A

4

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

What are the keys in this object?

let apartment = {
  coffeeMaker: 'Aeropress',
  ceilingFan: true,
  books: 114
}
What are the keys in this object?

coffeeMaker: ‘Aeropress’,
ceilingFan: true,
books: 114

‘Aeropress’,
true,
114

coffeeMaker,
ceilingFan,
books

A

coffeeMaker,
ceilingFan,
books

17
Q

What should we add to the empty .withDiscount() method to return the cost of the meatballs object with a two dollar discount?

let meatballs = {
    cost: 5,
    withDiscount() {
} }; What should we add to the empty `.withDiscount()` method to return the cost of the meatballs object with a two dollar discount?

this.cost - 2;

cost - 2;

return cost - 2;

return this.cost - 2;

A

return this.cost - 2;

18
Q

Is there anything wrong with setter method in the example below? If so, what?

let tempObj = {
    _num: 22,
    set(numIn) {
        _num = numIn;
    }
};
Is there anything wrong with setter method in the example below? If so, what?

There is nothing wrong with the method.

The setter input argument should be called num.

The setter should contain this._num in place of _num.

The num key should not have an underscore () in it.

A

The setter should contain this._num in place of _num.