JavaScript defines a standard class

Source: Internet
Author: User

JavaScript object-oriented programming is somewhat different from other OO languages. Therefore, when using JavaScript object-oriented features, you need to pay attention to some normative issues. Next, let's briefly talk about how JavaScript defines a class and how to standardize your code in the process of defining the class.

The following code defines classes using javascript:

Specify the class name and constructor. The first letter of the Class Name (constructor name) is uppercase:

function YourClass(){}

Use the "this. member variable" to define (pseudo) Private Members in its constructor. It is recommended that (pseudo) Private Members should all start with "_" and be composed of lowercase letters. This type of member is a different copy of each object, also called an object (Instance) member.

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 specify that such members must start with an uppercase letter (or the best Convention (pseudo) private Members are all lowercase letters starting .). This member variable is a copy shared by each object, also called a class member.

Yourclass. prototype. arg3 = "arg3... "; // define direct access. Do not enter the verified member variable Yourclass. prototype. _ arg4 = "arg4... "; // The setXXX () getXXX () accessors must be used for input verification.

Use the "class name. prototype. member function name = function (_ arga, _ argb,...) {}" method to add a member function.

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.