Objects Flashcards Preview

JavaScript Basics > Objects > Flashcards

Flashcards in Objects Deck (18)
Loading flashcards...
1

Introduction to JavaScript Objects

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

2

Change method to ES6:
let person = {
sayHello: () => {
return 'Hello, there!';
}
}

sayHello() {
return 'Hello, there!';
}

3

'this' keyword?

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

4

'this' example

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.

5

Getters and Setters: 3 advantages?

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.

6

Review: Objects

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.

7

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());

console.log(tempObj.num);

8

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

All of the options

9

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

restaurant.seatingCapacity

10

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 method is a function created inside of an object.

11

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.

Objects store data as key/value pairs.

12

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';

bikes['specialized'] = 'red';

13

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?

1

14

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']

myObj.sayHello()

15

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

4

16

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

coffeeMaker,
ceilingFan,
books

17

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;

return this.cost - 2;

18

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.

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