JavaScript objects
A JavaScript object is data that has properties and methods.
Objects, properties, and methods in real life
In real life, a car is an object.
An object has properties such as weight and color, and methods such as start and stop:
| object | property | Method |
|---|---|---|
![]() |
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 values differ from car to car.
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 JavaScript variables.
The following code sets the value of the variable car to "Fiat":
var
car =
"Fiat"
;
An object is also a variable, but an object can contain multiple values (multiple variables), each presented as a name:value pair.
var
car = {name:
"Fiat"
, model:
500
, color:
"white"
};
In the example above, three values ("Fiat", 500, "white") are assigned to the variable car.
![]() |
A JavaScript object is a container for variables. |
|---|
Object definition
You can use characters to define and create a JavaScript object:
Example
var
person = {firstName:
"John"
, lastName:
"Doe"
, age:
50
, eyeColor:
"blue"
};
Try it »
A JavaScript object definition can span multiple lines, and spaces and line breaks are optional:
Example
var
person = {
firstName:
"John"
,
lastName:
"Doe"
,
age:
50
,
eyeColor:
"blue"
};
Try it »
Object properties
It can be said that "a JavaScript object is a container for variables".
However, we usually think of "a JavaScript object as a container for key-value pairs".
A key-value pair is usually written as name : value (key and value separated by a colon).
Key-value pairs in a JavaScript object are usually called Object properties。
![]() |
A JavaScript object is a container for property variables. |
|---|
Writing 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:
Example 1
person.lastName;
Try it »
Example 2
person[
"lastName"
];
Try it »
Object methods
An object method defines a function and is stored as a property of the object.
An object method is called by adding () (as a function).
This example accesses the fullName() method of the person object:
Example
name = person.fullName();
Try it »
If you access the fullName property of the person object, it returns the string of the function definition:
Example
name = person.fullName;
Try it »
![]() |
A JavaScript object is a container 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:
Example
objectName
.
methodName
(
)
Try it »
Usually fullName() is a method of the person object, while fullName is a property.
If you use the fullName property without adding (), it returns the function definition:
Example
objectName
.
methodName
Try it »
There are many ways to create, use, and modify JavaScript objects.
There are also many ways to create, use, and modify properties and methods.
![]() |
In the following tutorials, you will learn more about objects. |
|---|

