Objects Flashcards
Introduction to JavaScript Objects
Represent real-world objects in JavaScript
Access object properties
Access object methods
Create object getter and setter methods
Change method to ES6: let person = { sayHello: () => { return 'Hello, there!'; } }
sayHello() {
return ‘Hello, there!’;
}
‘this’ keyword?
methods that operate on the data inside of the same object.
‘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.
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.
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.
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);
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
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
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.
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.
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’;
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
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()
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