As follows:
Copy Code code as follows:
function Student ()
{
Defines a field in the class student and assigns an initial value, but the access rights for this field are public
This.studentno = ' s001 ';
This.studentname = ' xiaoming ';
This.sex = ' Male ';
Defines the method Updatestudentname in the class student to modify the Studentname value
This.updatestudentname = function (studentname)
{
This.studentname = Studentname;
}
}
A student class is defined as the code above, and contains Studentno, Studentname,
Sex 3 fields, method Updatestudentname.
The following will be invoked with the following code:
Copy Code code as follows:
var s = new Student (); To create an object of the student class
Alert (' School No.: ' +s.studentno);
Alert (' Name: ' +s.studentname);
Alert (' Sex: ' +s.sex);
Before the Updatestudentname method is invoked, the number, name, and gender values are shown:
School Number: s001
Name: Xiao Ming
Sex: Male
Then call Updatestudentname to modify the value of the Studentname, the code is as follows:
Copy Code code as follows:
S.updatestudentname (' Xiao Qiang ');
Alert (' School No.: ' +s.studentno);
Alert (' Name: ' +s.studentname);
Alert (' Sex: ' +s.sex);
Again, the study number and gender will not change, the results are as follows:
School Number: s001
Name: Xiao Qiang
Sex: Male