JavaScript basics-Object-oriented programming <1>

Source: Internet
Author: User

1.1 Functions and objects

1. Defining a function in a way that defines a class

To define a method for a class:

function Class1 () { // class member definition and constructor section }

Class1 is both a function and a class.

Use the new operator to get an instance of a class

The new operator is not only valid for JavaScript internal objects, but can also be used for custom classes to get an instance.

For example:

1 var obj1=New Class1; // get an instance of Class1, which is an object obj1

Referencing the properties and methods of an object using point format and square brackets format

Object name. property or method name

The name of the object. ["Property or method name"]

For example:

1 var arr=New// Gets an array object instance 2 arr.push ("abc"); // To add an element to an array, push () is a method of array () 3 var // gets the length of the array, where length is the property of array () 4 // the length of the output array

Or

var New Array (); arr["Push"] ("abc"); var len=arr["Length"];alert (len);

This type of reference is analogous to an array of objects that represent a set of properties and methods.

Using the square brackets format to implement the call

<script type= "Text/javascript" >//defines a class user and includes a member of age and sex, specifying the initial value functionUser () { This. age=21;  This. sex= "Male";}varUser=NewUser ();//create an Object user //Display user information according to the following list options functionShow (SLT) {if(slt.selectindex!=0) {alert (User[slt.value]);//displays its values according to the property options, using the square brackets format to call   /*if using dot format, use if (slt.value= "age") alert (user.age);                  if (slt.value= "sex") alert (user.sex); */   }}<script>//Create the following list boxes to select and display information<select onchange= "Show (This)" > <option> Please select the information you need to see </option> <option value= "Age" > Ages </ option>//attribute options can be either <option value= "sex" > Gender </option></select>

JavaScript basics-Object-oriented programming <1>

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.