JavaScript Coding Specification

Source: Internet
Author: User
Tags class definition closure instance method

Variable Naming conventions:

Basic specification: Variable naming is made up of type prefixes + meaningful words, and the first letter of the word needs to be capitalized. For example: sUserName, ncount.

Prefix specification:

S: Represents a String. SName, sHtml

N: Represents a number. Npage, Ntotal

B: Representation logic. Bchecked

A: Represents an array. Alist

R: Represents the regular expression. Rdomain

F: Represents a function. Rgethtml

O: Indicates other objects not covered above: Obutton

Global variables use g as the prefix: gusername

Constants use all uppercase letters: COPYRIGHT, PI

function naming specification:

Unify the use of verbs or verbs [+ noun] forms, such as: Fgetversion (), Fsubmitform (), finit ();

Involving return logic is worth the function can use Is,has and so on to express logical words instead of verbs

If there is an intrinsic function, using the __f+ verb [+ noun] form, the intrinsic function must be defined at the end of the function

Object method Implementation:

The object method is named using the F+ object class name + verb [+ noun] form, such as: Faddressgetemail

Event Response Function:

An event response function is named by the trigger event object name + Event name or module name + Trigger event object name + event name, such as: Fdivclick (), Faddresssubmitbuttonclick ();

The definition class is done by a closure:

(function () {
The first step: introduce the existing classes. Introducing the Support class
var support = window. Support;

$ is a reference to the "base" of the NetEase Mailbox Base library will be introduced later
Step Two: Define the class. It can be assumed that a class definition function () {} is returned, and an image class is defined under Window
var Image = $.createclass ("image");

Can be thought of as the extend method of jquery

Step three: Define class Properties/Method definitions
$. Object.extend (image,{
_language:null,//internal properties
Getsize:fimagegetsize
});
Fourth step: defining instance Properties/Method definitions
$. Object.extend (image.prototype,{
Name:null,
Url:null,
Ext:null,
width:0,
height:0,
Setname:fimagesetname,
Getname:fimagegetname,
Init:fimageinit
});
Fifth Step: Method implementation

function Fimagegetsize (nwidth,nheight) {
return nwidth*nheight;
}

function Fimagesetname (sName) {
var oimage = this;
Oimage.name = SName;
}

function Fimagegetname () {
var oimage = this;
return oimage.name;
}

function Fimageinit (sURL) {
var oimage = this;
Oimage.url = sURL;
Oimage.ext = Support.getext (sURL);
Oimage.width = Support.getwidth (sURL);
Oimage.height = Support.getheight (sURL);
}

})();



As we can see, this closure completes the following several things:

1. Introduce the other classes that this class needs to use.

2. Define this class.

3. Define the properties and methods of the class.

4. Define instance properties and methods for the class.

5. Implementation of class and instance methods.

On the naming, we followed the rules:

1. The first letter of the class name must be capitalized, such as Image,support.

2. Attribute names need to be meaningful nouns, first letter lowercase, for example oimage.width.

3. Method name needs to be meaningful verb [+ noun], first letter lowercase, e.g. support.getwidth

4. If you do not want to be called by another method, you need to precede the property or method name with "_", for example Oimage._language

5. If you do not want the quilt class to be called, you need to add "_" before the property or method name, for example Oimage.__fire ()

The following points need to be specifically stated here:

1. The definition of a method is not defined by an anonymous function, but is concentrated within the class definition. The advantage of this is that the class's attribute method definitions are listed at the beginning, so that the corresponding properties and methods can be viewed through the source code.

2. In the class/instance method, use local variables instead of this. This is not a good thing, accidentally will be dizzy. The use of local variables can avoid such problems as much as possible, and can be more effective when compression is confused.

3. In the actual development process, each class definition is a single JS implementation.

In addition to the class definition, closures do not implement any other logic. Using closures can constrain many variables in the closure scope, and can be better in compression obfuscation.

  

JavaScript Coding Specification

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.