Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript objects

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.

Note 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.

Note 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。

Note A JavaScript object is a container for property variables.

Writing object key-value pairs is similar to:


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 »

 

Note 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.

Note In the following tutorials, you will learn more about objects.

More examples

Create JavaScript Objects I

Create JavaScript Objects II

Access Object Properties I

Access Object Properties II

Access a function property as a method

Access a function property as a property