Summary of defining class standards using javascript

Source: Internet
Author: User

The following code defines classes using javascript:

Specify the class name and constructor. The first letter of the Class Name (constructor name) is uppercase:
Copy codeThe Code is as follows:
Function YourClass (){
}

The "this. member variable" is used to define (pseudo) Private Members in the constructor. It is recommended that (pseudo) Private Members are composed of lowercase letters starting. This type of member is that each object has its own copy, also called an object (Instance) member.
Copy codeThe Code is as follows:
Function Yourclass (_ arg1, _ arg2 ,...){
This. _ arg1 = arg1;
This. _ arg2 = arg2;
//...
}

Use "class name. prototype. member variable "defines member variables outside its constructor. It is best to agree that such members should start with an uppercase letter (or the best Convention (pseudo) private Members are all in lowercase letters starting .). This member variable is a copy shared by each object, also called a class member.
Copy codeThe Code is as follows:
Yourclass. prototype. Arg3 = "arg3. .."; // define direct access. Do not enter a verified member variable.
Yourclass. prototype. _ arg4 = "arg4..."; // The setXXX () getXXX () accessors must be used for input validation.

Use the "class name. prototype. member function name = function (_ arga, _ argb,...) {}" method to add a member function.
Copy codeThe Code is as follows:
Yourclass. prototype. YourFucName = function (_ arga, _ argb ,...){
// Do somethings
}

Do not use "this. function name = function (_ arga ,....) {} "to define a member function. A function is a service template and needs to be shared. It is too wasteful and meaningless to store the same template for every object.

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.