Objects in JavaScript Flashcards
What are objects?
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.
What do objects hold?
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 do you separate each key/value in an object literal?
With a comma. For example:
const ledZeppelin = { singer: 'Robert Plant', guitar: 'Jimmy Page', bass: 'John Paul Jones', drums: 'John Bonham' }
When would you use quotations in a key within the object?
When you need a space or period in the key. For example:
const ledZeppelin2 = {
‘lead singer’: ‘Robert Plant’,
‘lead guitar’: ‘Jimmy Page’,
}
Can a key in an object be used more than once?
No, each key must be unique in the object.
What types of values can a key have?
Any valid JavaScript data type, including numbers, strings, booleans, other objects, null, or functions.
What is a method?
When an object has a value that is a function, the key/value pair is known as a method.
How do you get values or run object methods?
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 do you add new keys to an object?
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 do you update values in a key?
Just like how you add a new key/value pair.
How do you delete a key/value pair in an object?
Use the delete command. For example:
const foo = { bar: true }; delete foo.bar; console.log(foo.bar); // => undefined
What is self reference?
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 do you self reference with the keyword ‘this’?
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.
Why should you be careful when writing functions that take objects as an argument?
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 do you iterate through key/value pairs in an object?
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.
);
});