Most classes in javascript are represented by function functions, and most of them are defined in json format later. Classes in javascript are represented by function functions, as follows:
The Code is as follows:
Function Student ()
{
// Define the field in the Student class and grant the initial value. However, the access permission for this field is public.
This. studentNo = 's001 ';
This. studentName = 'xiaoming ';
This. sex = 'male ';
// Define the method updateStudentName in the Student class to modify the value of studentName
This. updateStudentName = function (studentName)
{
This. studentName = studentName;
}
}
// The code above defines a Student class and includes studentNo,
// StudentName, sex three fields, method updateStudentName. // then call updateStudentName to modify the value of studentName. The Code is as follows:
S. updateStudentName ('xiaoqiang ');
Alert ('student ID: '+ s. studentNo );
Alert ('name: '+ s. studentName );
Alert ('gender: '+ s. sex );
// The result is displayed. The student ID and gender will not change. The result is as follows:
Student ID: s001
Name: Xiaoqiang
Gender: male
// The student ID, name, and gender values are displayed before the updateStudentName method is called:
Student ID: s001
Name: James
Gender: male
// Call the following code:
The Code is as follows:
Var s = new Student (); // create a student Class Object
Alert ('student ID: '+ s. studentNo );
Alert ('name: '+ s. studentName );
Alert ('gender: '+ s. sex );
The specific values have been set in the above function. In fact, they are all assigned values in practical applications. For example
Script function Student (studentNo, studentName, sex) {// defines the field in Student class and assigns the initial value, but the access permission for this field is public this. studentNo = studentNo; this. studentName = studentName; this. sex = sex; // defines the method updateStudentName in the Student class, which is used to modify the value of studentName this. updateStudentName = function (studentName) {this. studentName = studentName;} var s = new Student ("001", "James", "male"); // create the student Class Object alert ('student ID: '+ s. studentNo); alert ('name: '+ s. studentName); alert ('gender: '+ s. sex); s. updateStudentName (""); // modify the name alert ('student ID: '+ s. studentNo); alert ('name: '+ s. studentName); alert ('gender: '+ s. sex); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]