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.