Document directory
- Attribute
- Method
- 1. Create an instance of the object
- 2. Create an object Template
Objects help organize information.
Instance
-
Instance of the created object
-
This example shows how to create an instance of the javacripobject ).
-
Create an object Template
-
This example shows you how to create a template for a javacript object ).
JavaScript Object
In the previous section of this tutorial, we have learned that JavaScript has several built-in objects, such as string, date, and array. In addition to these objects, you can also create your own objects.
An object is only a special data type and has a series of attributes and methods.
Let's use an example to understand that a person is an object. Properties are values related to objects. A person's attributes include his/her name, height, weight, age, skin color, and eye color. All people have these attributes, but their values are different. Objects also have methods. A method is a behavior that can be applied to an object. A person's approach may be eating, sleeping, working, and playing.
Attribute
Method for accessing object attributes:
Object Name. attribute name
By simply assigning values to attributes, you can add attributes to objects. Assume that the object personobj exists. You can add attributes such as firstname, lastname, age, and eyecolor.
personObj.firstname="John"personObj.lastname="Doe"personObj.age=30personObj.eyecolor="blue"document.write(personObj.firstname)
The above code generates the following output:
John
Method
The object can contain methods.
Use the following syntax to call the method:
Object Name. Method Name ()
Note: parameters used for methods between parentheses can be omitted.
Call the method of the personobj object named sleep:
personObj.sleep()
Create your own object
There are several different ways to create objects:
1. Create an instance of the object
The following code creates an object instance and adds four attributes to it:
personObj=new Object()personObj.firstname="John"personObj.lastname="Doe"personObj.age=50personObj.eyecolor="blue"
Adding Methods to personobj is also very easy. The following code adds a method named eat () to personobj:
personObj.eat=eat
2. Create an object Template
The template defines the object structure.
function person(firstname,lastname,age,eyecolor){this.firstname=firstnamethis.lastname=lastnamethis.age=agethis.eyecolor=eyecolor}
Note: The template is just a function. You need to allocate content to this. propertiname in the function.
Once you have a template, you can create a new instance like this:
myFather=new person("John","Doe",50,"blue")myMother=new person("Sally","Rally",48,"green")
You can also add some methods to the person object. You also need to perform operations in the template:
function person(firstname,lastname,age,eyecolor){this.firstname=firstnamethis.lastname=lastnamethis.age=agethis.eyecolor=eyecolorthis.newlastname=newlastname}
Note: methods are only attached to functions of objects. Then, we need to compile the newlastname () function:
function newlastname(new_lastname){this.lastname=new_lastname}
The newlastname () function defines the new lastname of person and assigns it to person. By using "this.", JavaScript can tell you who you are referring. So now you can write: Mymother. newlastname ("doe ").