Home  >  Article  >  Web Front-end  >  Summary of specifications for defining classes with javascript_javascript skills

Summary of specifications for defining classes with javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:15:541117browse

The specifications for using JavaScript to define a class are as follows:

Specify the class name and constructor. The first letter of the class name (constructor name) is capitalized:

Copy code The code is as follows:

function YourClass(){
}

Use "this.member variable" in its constructor To define (pseudo) private members internally, it is best to agree that (pseudo) private members are composed of lowercase letters starting with "_". This kind of member is that each object has its own copy, also called object (instance) member.
Copy code The code is as follows:

function Yourclass(_arg1,_arg2,...){
this._arg1=arg1;
this._arg2=arg2;
//...
}

Use "class name.prototype.member variable" in When defining member variables outside its constructor, it is best to stipulate that such members all start with a capital letter (or it is best to stipulate that (pseudo) private members all start with a lowercase letter starting with "_".). This kind of member variable is a copy shared by each object, also called a class member.
Copy code The code is as follows:

Yourclass.prototype.Arg3="arg3..." ;//Define direct access to member variables that do not require input verification
Yourclass.prototype._arg4="arg4...";//You need to use setXXX() getXXX() accessor for input verification

Use "class name.prototype.member function name=function(_arga,_argb,...){}" to create member functions.
Copy code The code is as follows:

Yourclass.prototype.YourFucName=function(_arga,_argb, ...){
//do somethings
}

Do not use "this.function name=function(_arga,....){}" in the constructor To define member functions, functions are service templates and need to be shared. There is no need to store the same template for each object. This is too wasteful and makes little sense.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn