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>