Objects Flashcards
What are objects in JavaScript?
An object is data that contains key:value pairs
How do we create objects in JavaScript
We create objects by containing the key:value pairs within curly braces. Multiple K:V need to be separated by commas
How can we conceptualize the relationship between key and value in an object
Each key references a value
Much like a word and its definition in the dictionary
What values can we put in an object?
Objects can contain any valid data structure - Primitives and Objects
What is another name for keys in an JavaScript object?
Keys are also known as
Properties
How can we get the value of an object property (key)?
We can use dot (.) operator (dot notation)
or
bracket [] notation
Give the syntax for using dot notation to get the value of an object property.
objectName.propertyName
aka
objectName.keyName
Give the syntax for using bracket notation to get the value of an object’s property
objectName[‘propertyName’]
note that the propertyName is a string
When do you need to use bracket notation to access the value of an object’s properties?
- When you access the value of a property with a variable.
- When the property name is an invalid identifier
How do we set the value of a property (key) in an object?
We can use either dot notation (preferred) or bracket notation
Give the syntax for setting an object property value.
objectName.propertyName = value
we can use bracket notation too
How do we delete an object key:value pair?
delete objectName.propertyName
we can use bracket notation too
If two objects hold the same information will the equate to equal using comparison opperators?
No, objects are compared by reference, not by content.
When will two variables that point to objects be equal using equality opperators?
Only when variables point to the same object, or one variable points to a variable pointing to an object, will they be equal.
When do keys (or identifiers) need quotation marks in an object literal?
Then the key or identifier contains special characters (like a space).
How do we create a method in an object literal?
Two ways:
- Set the method name with the key, and the value as an anonymous function after the colon
- Set the method name with the key, and create the function without the colon or function name
Give the syntax for the ES6 method creation in an object.
const objectName = {
methodName () { method code block } }