Create your own JavaScript Object

Source: Internet
Author: User
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 " );
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.