One of javascript object-oriented definition classes

Source: Internet
Author: User

Classes in javascript are represented by function functions, as follows:
Copy codeThe 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:
Copy codeThe 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

[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.