SoFunction
Updated on 2025-04-06

javascript object-oriented function class

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;
}
}

As mentioned above, a Student class has been defined and contains studentNo, studentName,
sex 3 fields, method updateStudentName.
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 student number, name, and gender values ​​are displayed before the updateStudentName method are:
Student ID: s001
Name: Xiao Ming
Gender: Male

Then call updateStudentName to modify the value of studentName, the code is as follows:
Copy the codeThe code is as follows:

('Xiaoqiang');
alert('Student number:'+);
alert('Name:'+);
alert('Gender:'+);

When the results are displayed, the student number and gender will naturally not change, and the results are as follows:
Student ID: s001
Name: Xiaoqiang
Gender: Male