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.