As follows:
function Student()
{
//Define the field in class Student and assign it an initial value, but the access permission of this field is public
this.studentNo = 's001';
this.studentName = 'Xiao Ming';
this. sex = 'Male';
//Define the method updateStudentName in class Student, used to modify the studentName value
this.updateStudentName = function(studentName)
{
this.studentName = studentName;
}
}
The above code has defined a Student class, and contains 3 fields: studentNo, studentName,
sex, and the method updateStudentName.
The following will be called, The code is as follows:
var s = new Student(); / /Create an object of student class
alert('Student number:' s.studentNo);
alert('Name:' s.studentName);
alert('Gender:' s.sex);
Before the updateStudentName method is called, the values of student ID, name, and gender are displayed:
Student ID: s001
Name: Xiao Ming
Gender: Male
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);
Display the results again. The student ID and gender will naturally not change. The results are as follows:
Student ID: s001
Name: Xiaoqiang
Gender: Male