Home > Web Front-end > JS Tutorial > javascript object-oriented function class_js object-oriented

javascript object-oriented function class_js object-oriented

WBOY
Release: 2016-05-16 18:27:31
Original
936 people have browsed it

As follows:

Copy code The code is as follows:

function Student()
{
//Define the field in class Student and assign it an initial value, but the access permission of this field is public
this.studentNo = 's001';
this.studentName = 'Xiao Ming';
this. sex = 'Male';
//Define the method updateStudentName in class Student, used to modify the studentName value
this.updateStudentName = function(studentName)
{
this.studentName = studentName;
}
}

The above code has defined a Student class, and contains 3 fields: studentNo, studentName,
sex, and the method updateStudentName.
The following will be called, The code is as follows:
Copy code The code is as follows:

var s = new Student(); / /Create an object of student class
alert('Student number:' s.studentNo);
alert('Name:' s.studentName);
alert('Gender:' s.sex);

Before the updateStudentName method is called, the values ​​of student ID, name, and gender are displayed:
Student ID: s001
Name: Xiao Ming
Gender: Male

Then call updateStudentName to modify the value of studentName. The code is as follows:
Copy the code The code is as follows:

s.updateStudentName('Xiaoqiang');
alert('Student ID:' s.studentNo);
alert('Name:' s.studentName);
alert('Gender:' s. sex);

Display the results again. The student ID and gender will naturally not change. The results are as follows:
Student ID: s001
Name: Xiaoqiang
Gender: Male
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template