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.
- A Boolean can be an object.
- A number can be an object.
- A string can also be an object
- A date is an object
- Math and regular expressions are also objects
- An array is an object
- Even a function can be an object
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:
- Use Object to define and create an instance of an object.
- Use a function to define an object, then create new object instances.
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:
- If the given value is null or undefined, an empty object is created and returned.
- If a primitive value is passed in, an object of its wrapper type is constructed.
- If a reference value is passed in, that value is returned; variables copied from it hold the same reference address as the source object.
- When called as a function rather than a constructor, Object behaves the same as new Object().
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...
};
- myObject: The variable name used to refer to the whole object.
- key1, key2: The property name, which can be a string or an identifier.
- value1, value2: The property value, which can be any JavaScript data type, including number, string, Boolean, function, array, or even another object.
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 »