Javascript object-oriented definition class _ js object-oriented-js tutorial

Source: Internet
Author: User
Most classes in javascript are represented by function functions, and most of them are defined in json format later. Classes in javascript are represented by function functions, as follows:

The Code is as follows:


Function Student ()
{
// Define the field in the Student class and grant the initial value. However, the access permission for this field is public.
This. studentNo = 's001 ';
This. studentName = 'xiaoming ';
This. sex = 'male ';
// Define the method updateStudentName in the Student class to modify the value of studentName
This. updateStudentName = function (studentName)
{
This. studentName = studentName;
}
}


// The code above defines a Student class and includes studentNo,
// StudentName, sex three fields, method updateStudentName. // then 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 );
// The result is displayed. 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
// Call the following code:

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


The specific values have been set in the above function. In fact, they are all assigned values in practical applications. For example

Script function Student (studentNo, studentName, sex) {// defines the field in Student class and assigns the initial value, but the access permission for this field is public this. studentNo = studentNo; this. studentName = studentName; this. sex = sex; // defines the method updateStudentName in the Student class, which is used to modify the value of studentName this. updateStudentName = function (studentName) {this. studentName = studentName;} var s = new Student ("001", "James", "male"); // create the student Class Object alert ('student ID: '+ s. studentNo); alert ('name: '+ s. studentName); alert ('gender: '+ s. sex); s. updateStudentName (""); // modify the name alert ('student ID: '+ s. studentNo); alert ('name: '+ s. studentName); alert ('gender: '+ s. sex); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.