Classes in javascript are represented by function functions, as follows:
Copy the codeThe code is as follows:
function Student()
{
//Define the field in the class Student and assign the initial value, but the access permission of this field is public
= 's001';
= 'Xiao Ming';
= 'Male';
//Define the method updateStudentName in class Student to modify the studentName value
= function(studentName)
{
= studentName;
}
}
//The above code has defined a Student class and contains studentNo.
//studentName, sex 3 fields, method updateStudentName. //Then call updateStudentName to modify the value of studentName. The code is as follows:
('Xiaoqiang');
alert('Student number:'+);
alert('Name:'+);
alert('Gender:'+);
//Show the results again, the student number and gender will naturally not change, the results are as follows:
Student ID: s001
Name: Xiaoqiang
Gender: Male
//The student number, name and gender values are displayed before the updateStudentName method are:
Student ID: s001
Name: Xiao Ming
Gender: Male
//The following will be called, the code is as follows:
Copy the codeThe code is as follows:
var s = new Student(); //Create an object of the student class
alert('Student number:'+);
alert('Name:'+);
alert('Gender:'+);
The specific values have been set in the above function, but in actual applications, they are all assigned later. For example
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]