As follows:
CopyCode 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.
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