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. 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

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 = 30 personObj. 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 = 50 personObj. 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 = firstname this. lastname = lastname this. age = age this. 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 = firstname this. lastname = lastname this. age = age this. eyecolor = eyecolor this. 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 ").

 

From youhaoxinqin's column

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.