It can be tedious to define separate variables when you have a collection of related information. Instead of defining each variable separately, you can create an object and list the variables as properties of the object. This makes it easier to manipulate all the variables. (Note that variables that are part of an object are called "properties" instead of "variables").
Quick Review of Objects
As a review, remember that arrays are objects. As an object, you can apply methods to them. For example:
The result is 5. There are 5 positions in the array.
Here's how you might use a method on a date object:
The result is the current day, which in this case is 1, or Monday.
Creating New Objects
You can create a new object that isn't an already an array or date object. To create a new object, define a new variable and assign it as a
Once you have defined the new object, you can set up the various properties of the object. You list these properties by adding a dot and then the property name.
It's convenient to list each of these highly-related variables as properties of an object, rather than listing them out as separate variables.
You can use a shorthand syntax for creating an object and listing its properties. Rather than listing properties separately on their own lines, you can group them inside curly braces, separated by commas:
Whenever you need to call any of the variables, you list the object name followed by
.property, like this:
The result would be 7.
this for exactly this purpose.
Here's an example of how you might integrate
This will return "Panthers has a AAAA rating" as well as "Lions has a AA rating."
Both team1 and team2 refer to the same function,
teamProfile. When the function runs, because it uses
this instead of something more specific,
team2 will pass into
Properties and Methods
Objects can have both properties and methods. A property holds data about the object, whereas a method performs some action on the object. Both properties and methods are accessed through the dot operator that follows the object's name.
Here's an example:
This variable will return the length of the message object. In this case, length tells us a property of the object.
A method will perform some action usually with the object's properties.
Objects can have different data types.
Arrays are a data type that holds multiple values.
You create a new array data type with the following constructor:
This creates a new array and stores several values in it:
There's also what's known as "literal notation" that involves brackets instead:
Each position in the array's values is called "index notation." The index refers to the position of the value in the array. It starts at 0 and increments from there.
There are a handful of useful methods to use with arrays:
- concat (array1, array2): joins multiple arrays
- join (separator): converts the array into a string
- pop: removes the last array element
- push (element): adds a new element to the array
- reverse: reverses the order of the array's elements
- sort: sorts the elements of the array into alphabetical order
new Date() constructor is used to create a new date object like this:
You can also create a date object by passing it some date information like this:
The following are some methods for working with the Date object.
Here are some methods related to time:
I'm not going to go into more detail, but you can work with the Number and the String object data types in depth as well.
Creating custom objects
You can create a new object in a couple of ways. This way would create a new object and set it as a variable called coordinates:
There's another method that uses something called "literal notation" to accomplish the same thing:
Recall that you can create an array object using the "array literal" shorthand, which involves using brackets. With new objects, you use curly braces instead.
Adding Properties to a New Object
Here's how you add properties to the new object:
You could also create the new object and add its properties in one step:
The x and y are property definitions for the coordinates object.
After you have set up the object, you can use it just like any other object:
Define a Method to Use with a New Object
You can also define a method for a new object. You do this in almost the same way that you list a property for the object, except that you write it as a function:
You'll notice that we're using "this" in the function that is written as a method for the coordinates object. The "this" term means "this object" and refers to the coordinates object. Because this function appears within the coordinates object, the "this" refers to the coordinates object. "This" means "this object."
Passing Objects as Arguments in a Function
You can even pass objects as arguments in a function. For example, suppose you have this function:
You can actually pass the function as an argument, like this:
I'd Rather Be Writing Newsletter
Get new posts delivered straight to your inbox.