JavaScript to define specifications for classes summary _javascript tips

Source: Internet
Author: User
Tags lowercase uppercase letter
Use JavaScript to define the specification of a class as follows:

Specify the class name and the constructor, and the class name (the constructor name) to capitalize the first letter:
Copy Code code as follows:

function YourClass () {
}

Use this. Member variable to define (pseudo) private members within its constructor, and the best convention (pseudo) Private members are made up of lowercase letters with "_". This type of member is that each object has its own copy, also called an object (instance) member.
Copy Code code as follows:

function YourClass (_arg1,_arg2,...) {
THIS._ARG1=ARG1;
THIS._ARG2=ARG2;
//...
}

Use the class name. prototype. member variable to define a member variable outside its constructor, preferably by agreeing that such a member starts with an uppercase letter (or the best convention (pseudo) Private member is a lowercase letter with "_"). )。 This member variable is a single copy of each object, also called a class member.
Copy Code code as follows:

Yourclass.prototype.arg3= "Arg3 ..."//define direct access do not enter a positive member variable
Yourclass.prototype._arg4= "Arg4 ...";/need to use SETXXX () getxxx () accessor to do input verification

Use the class name. prototype. member function name =function (_arga,_argb,...) {} "way to member functions.
Copy Code code as follows:

Yourclass.prototype.yourfucname=function (_arga,_argb,...) {
Do somethings
}

Do not use the this. function name =function (_arga,....) in constructors {} "way to define a member function, which is a service template that needs to be shared, and does not require that each object have an identical template, which is too wasteful and of little significance.
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.