JavaScript Objects
JavaScript objects are data that have properties and methods.
Real-Life Objects, Properties, and Methods
In real life, a car is an object.
Objects have their properties, such as weight and color, and methods such as start and stop:
| Object | Properties | Methods |
|---|---|---|
|
car.name = Fiat car.model = 500 car.weight = 850kg car.color = white |
car.start() car.drive() car.brake() car.stop() |
All cars have these properties, but the properties of each car are not exactly the same.
All cars have these methods, but they are executed at different times.
JavaScript Objects
In JavaScript, almost everything is an object.
![]() |
In JavaScript, objects are very important. Once you understand objects, you can understand JavaScript. |
|---|
You have already learned about assigning values to JavaScript variables.
The following code assigns to the variablecarthe value "Fiat":
An object is also a variable, but an object can contain multiple values (multiple variables), and each value is presented as aname:valuepair.
In the above example, three values ("Fiat", 500, "white") are assigned to the variable car.
![]() |
JavaScript objects are containers for variables. |
|---|
Object Definition
You can use a literal to define and create JavaScript objects:
Defining a JavaScript object can span multiple lines; spaces and line breaks are not required:
Example
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
Try it Yourself »
Object Properties
It can be said that "JavaScript objects are containers for variables".
However, we usually think of "JavaScript objects as containers for key-value pairs".
Key-value pairs are usually written asname : value(the key and value are separated by a colon).
Key-value pairs in JavaScript objects are usually calledobject properties.。
![]() |
JavaScript objects are containers for property variables. |
|---|
The writing of object key-value pairs is similar to:
- associative arrays in PHP
- dictionaries in Python
- hash tables in C
- hash maps in Java
- hash tables in Ruby and Perl
Accessing Object Properties
You can access object properties in two ways:
Object Methods
An object method defines a function and stores it as a property of the object.
Object methods are called by adding () (as a function).
This example accesses the fullName() method of the person object:
If you access the fullName property of the person object, it will be returned as a string defining the function:
![]() |
JavaScript objects are containers for properties and methods. |
|---|
In the following tutorials, you will learn more about functions, properties, and methods.
Accessing Object Methods
You can create an object method using the following syntax:
methodName : function() {
// 代码
}
You can access an object method using the following syntax:
Usually fullName() is a method of the person object, and fullName is a property.
If you use the fullName property, without adding(), it will return the definition of the function:
There are many ways to create, use, and modify JavaScript objects.
Similarly, there are many ways to create, use, and modify properties and methods.
![]() |
In the following tutorials, you will learn more about objects. |
|---|
