JavaScript: Objects Flashcards

1
Q

What is an object?
What are properties?
What are key-value pairs?
What is the syntax?

A

Objects are a collection of properties.

const planet = {
// Properties are made up of key-value pairs
name: “Earth”,
age: “4.543 billion years”,
moons: 1,
isPopulated: true,
population: “7.594 billion”,
};

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

How do you access a property’s value?
What is the syntax?

A

const planet = {
name: “Earth”,
age: “4.543 billion years”,
moons: 1,
isPopulated: true,
population: “7.594 billion”,
};

// To access a property’s value that is a string, number or boolean, use the object’s name and the associated key
// Uses dot notation and logs “Earth”

console.log(planet.name);

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

What is the second way to access a property’s value?
What is the syntax?

A

const planet = {
name: “Earth”,
age: “4.543 billion years”,
moons: 1,
isPopulated: true,
population: “7.594 billion”,
};

// Uses bracket notation and logs “Earth”
console.log(planet[“name”]);

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

Example of Array and Methods Inside an Object

A

var planet = {
name: “Earth”,
age: “4.543 billion years”,
moons: 1,
isPopulated: true,
population: “7.594 billion”,
// Objects can store arrays in a key-value pair
neighboringPlanets: [“Mars”, “Venus”],
// Objects can also store methods
tellFunFact: function () {
console.log(“The earth is the only planet in our solar system not named after a Roman god or goddess.”);
},
showWarning: function () {
console.log(“Space junk falls into Earth’s atmosphere at a rate of about one a day.”);
}
};

// To access a value in an array, use the name of the object, the key and the index.
// Logs “Mars” using dot notation
console.log(planet.neighboringPlanets[0]);

// Logs “Mars” using bracket notation
console.log(planet[“neighboringPlanets”][0]);

// To call a method, use the name of the object and the key. Don’t forget the ()!
planet.tellFunFact();
planet.showWarning();

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

The default keyword _______ refers to the global object. In a browser, the global object is the _________.

A

The default keyword “this” refers to the global object. In a browser, the global object is the Window.

// Logs Window
console.log(this);

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

Example of “this” in an Object

A

var planet = {
name: “Earth”,
age: “4.543 billion years”,
isPopulated: true,
population: “7.594 billion”,
logFacts: function () {
//Logs “This planet’s name is Earth”
console.log(“This planet’s name is “ + this.name);
//Logs “This planet’s age is 4.543 billion years”
console.log(“This planet’s age is “ + this.age);
},
logPopulation: function () {
if (this.isPopulated) {
// Logs “This planet’s population is 7.594 billion”
console.log(“This planet’s population is “ + this.population);
} else {
console.log(“The planet is unpopulated”);
}
}
};

// Calls object methods
planet.logFacts();
planet.logPopulation();

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