Classes in JavaScript are represented by function functions, as follows:
Function Student ()
{
// Define the field in student class and assign the initial value. However, the access permission for this field is public.
This . Studentno = ' S001 ' ;
This . Studentname = ' James ' ;
This . Sex = ' Male ' ;
// Defines the method updatestudentname in the student class, which is used to modify the value of studentname
This . Updatestudentname = Function (Studentname)
{
This . Studentname = Studentname;
}
}
// AboveCodeYou have defined a student class and included studentno,
// Studentname, Sex three fields, method updatestudentname. // 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 );
// Then, 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
// 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 );