JavaScript Object: an object is only a special data type and has a series of attributes and methods.
General example:
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.
Method for accessing object attributes: Object Name. attribute name
For example:
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 = " Wuwei " ;
Personobj. lastname = " Studio " ;
Personobj. Age = 25 ;
Personobj. eyecolor = " Black " ;
Document. Write (personobj. firstname );
Output: Wuwei
Call method: Object Name. Method Name ()
For example, call the method of the personobj object named sleep: personobj. Sleep ();
Create an object of your own: There are several different ways to create an object.
1. Create an instance of an object , for example, personobj = New Object ()
personobj. firstname = " Wuwei " ;< br> personobj. lastname = " studio ";
personobj. age = 25 ;< br> personobj. eyecolor = " black ";
2.Creates an object template, which defines the object structure. Note: The template is just a function.For example:FunctionPerson (firstname, lastname, age, eyecolor)
{
This. Firstname=Firstname;
This. Lastname=Lastname;
This. Age=Age;
This. Eyecolor=Eyecolor;
}
Once you have a template, you can create a new instance., Like this:
Myfather = New Person ( " Bwuwei " , " Bstudio " , 50 , " Black " );
Mymother = New Person ( " Gwuwei " , " Gstudio " , 48 , " Black " );