JavaScript Objects Worksheet
Question 1
What makes up an object (what does an object consist of)?
An object is a collection of properties that describe the object, as well as methods that describe the behavior of the object.
A property consists of a name (or key) and its value.
For example, name: "Name of Object"
A method is a property whose value is a function.
For example: movement: function(){Action/Behavior of Object's Movement}; or movement(){Action/Behavior of Object's Movement};
A property consists of a name (or key) and its value.
For example, name: "Name of Object"
A method is a property whose value is a function.
For example: movement: function(){Action/Behavior of Object's Movement}; or movement(){Action/Behavior of Object's Movement};
Question 2
What are two different types of notations that you can use when working with the properites of objects? For example, there are two types of syntax that you could use if you wanted to update a property of an object.
Let's say that the object myDog has name property set to "Miles".
To update the name property outside of the object, you could use either of the following notations:
-
Dot Notation
- myDog.name = "Rufus"
-
Bracket Notation
- myDog['name'] = "Rufus"
Question 3
Why are objects an important data type in JavaScript?
Objects allow us to make models in our code that simulate real-world objects, which assists in data organization.
Question 4
When creating an object, what character is used to separate property names from their values?
The colon character (:) is used to separate property names from their value.
For example: name: "Name of Object"
For example: name: "Name of Object"
Question 5
When creating an object, what character is used to separate the property/value pairs in an object?
The comma character (,) is used to separate each of the property/value pairs in an object.
For example: let myDog = {name: "Miles", breed: "Golden Retriever", age: 8};
For example: let myDog = {name: "Miles", breed: "Golden Retriever", age: 8};
Question 6
What operator is used to access a property of an object?
The dot operator is used to access a property of an object.
For example: console.log(myDog.name);. This will show the object myDog's name property of "Miles" in the console log, when using the object's information from the question above.
For example: console.log(myDog.name);. This will show the object myDog's name property of "Miles" in the console log, when using the object's information from the question above.