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

JavaScript objects

JavaScript objects


Everything in JavaScript is an object: strings, numbers, arrays, functions...

In addition, JavaScript allows you to define your own objects.


Everything is an object

JavaScript provides several built-in objects, such as String, Date, Array, and so on. An object is simply a special data type with properties and methods.


JavaScript objects

An object is just a special kind of data. Objects havepropertyandMethod。


Accessing object properties

Properties are values associated with an object.

The syntax for accessing an object property is:

objectName.propertyName

This example uses the String object's length property to get the length of a string:

var message="Hello World!";

var x=message.length;

After the code above is executed, the value of x will be:

12


Accessing object methods

Methods are actions that can be performed on an object.

You can call a method using the following syntax:

objectName.methodName()

This example uses the String object's toUpperCase() method to convert text to uppercase:

var message="Hello world!";

var x=message.toUpperCase();

After the code above is executed, the value of x will be:

HELLO WORLD!


Creating JavaScript objects

With JavaScript, you can define and create your own objects.

There are two different ways to create a new object:

Using Object

In JavaScript, almost all objects are instances of the Object type, and they all inherit properties and methods from Object.prototype.

The Object constructor creates an object wrapper.

The Object constructor creates an object based on the given argument, as follows:

Syntax format:



// Call as a constructor

new Object([value])

value can be any value.

The following example uses Object to create a Boolean object:

// 等价于 o = new Boolean(true);

var o = new Object(true);

This example creates a new instance of an object and adds four properties to it:

Example

person

=

new

Object

(

)

;

person

.

firstname

=

"

John

"

;

person

.

lastname

=

"

Doe

"

;

person

.

age

=

50

;

person

.

eyecolor

=

"

blue

"

;

Try it »

You can also use an object literal to create an object, with the following syntax:



var myObject = {

    key1: value1,

    key2: value2,

    // More key-value pairs...

};

Example

var

person

=

{

    firstName

:

'John'

,

    lastName

:

'Doe'

,

    age

:

30

,

    isStudent

:

false

,

    greet

:

function

(

)

{

        console.

log

(

'Hello, I am '

this

.

firstName

' '

this

.

lastName

)

;

   

}

}

;

console.

log

(

person.

firstName

)

;

 

// Output: John

person.

greet

(

)

;

 

// Output: Hello, I am John Doe

Example

person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};

Try it »

A JavaScript object is a name:value collections.


Using an object constructor

This example uses a function to construct an object:

Example

function

person

(

firstname

,

lastname

,

age

,

eyecolor

)

{

this

.

firstname

=

firstname

;

this

.

lastname

=

lastname

;

this

.

age

=

age

;

this

.

eyecolor

=

eyecolor

;

}

Try it »

In JavaScript, this usually refers to the function being executed, or to the object to which the function belongs (at runtime)


Creating JavaScript object instances

Once you have an object constructor, you can create new object instances like this:

var myFather=new person("John","Doe",50,"blue");

var myMother=new person("Sally","Rally",48,"green");


Adding properties to a JavaScript object

You can add new properties to an existing object by assigning values to it:

Assuming the person object already exists - you can add these new properties to it: firstname, lastname, age, and eyecolor:

person.firstname="John";

person.lastname="Doe";

person.age=30;

person.eyecolor="blue";



x=person.firstname;

After the code above is executed, the value of x will be:

John


Adding methods to a JavaScript object

Methods are simply functions attached to an object.

Define an object's method inside the constructor function:



function person(firstname,lastname,age,eyecolor)

{

    this.firstname=firstname;

    this.lastname=lastname;

    this.age=age;

    this.eyecolor=eyecolor;



    this.changeName=changeName;

    function changeName(name)

    {

        this.lastname=name;

    }

}

The changeName() function assigns the value of name to the person's lastname property.

Now you can try it:

myMother.changeName("Doe");

Try it »


JavaScript classes

JavaScript is an object-oriented language, but JavaScript does not use classes.

In JavaScript, classes are not created, and objects are not created from classes (as in other object-oriented languages).

JavaScript is based on prototypes, not on classes.


JavaScript for...in loop

The JavaScript for...in statement loops through the properties of an object.

Syntax



for (variable in object)

{

    执行的代码……

}

**Note: **the code block in a for...in loop is executed once for each property.

Example

Loop through the properties of an object:

Example

var

person

=

{

fname

:

"

John

"

,

lname

:

"

Doe

"

,

age

:

25

}

;

for

(

x

in

person

)

{

txt

=

txt

person

[

x

]

;

}

Try it »


JavaScript objects are mutable

Objects are mutable and are passed by reference.

The person object in the following example is not copied:

var x = person;  // 不会创建 person 的副本,是引用

If you modify x, the properties of person also change:

Example

var

person

=

{

firstName

:

"

John

"

,

lastName

:

"

Doe

"

,

age

:

50

,

eyeColor

:

"

blue

"

}

var

x

=

person

;

x

.

age

=

10

;

//

Both x.age and person.age change

Try it »