As follows:
Copy codeThe 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.
The Code is as follows:
Copy codeThe 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 student ID, name, and gender values are displayed before the updateStudentName method is called:
Student ID: s001
Name: James
Gender: male
Call updateStudentName to modify the value of studentName. The Code is as follows:
Copy codeThe Code is as follows:
S. updateStudentName ('xiaoqiang ');
Alert ('student ID: '+ s. studentNo );
Alert ('name: '+ s. studentName );
Alert ('gender: '+ s. sex );
Then, the student ID and gender will not change. The result is as follows:
Student ID: s001
Name: Xiaoqiang
Gender: male