JavaScript Coding Specification

Source: Internet
Author: User

1 Code Style

1.1 Structure Statement

[ mandatory ] You must not omit the semicolon at the end of the statement.

[ mandatory ] in the if/else/for/do/while statement, even if there is only one row, you must not omit the block {...} .

Example:

// Good if (condition) {    callfunc ();} //  Bad if (condition) callfunc (); if (condition)    callfunc ();

1.2 naming

[ mandatory ] variables use the Camel name Method .

Example:

var loadingmodules = {};

[ mandatory ] Constants use all uppercase letters to name the words that are separated by an underscore.

Example:

var Html_entity = {};

[ mandatory ] function use the Camel name Method .

Example:

function StringFormat (source) {}

[ mandatory ] of the function The parameters use the Camel name Method .

Example:

function Hear (thebells) {}

[ mandatory ] class use Pascal to name the method .

Example:

function Textnode (options) {}

[ mandatory ] Class of method / Property uses the Camel name Method .

Example:

function Textnode (value, engine) {    this. Value = value;      this. Engine =function  () {    return This ;};

[ mandatory ] Enumeration Variables using the Pascal nomenclature , enumerated properties use the All letters are capitalized, and the names of the words are separated by an underscore.

Example:

var Targetstate = {    1,    2,    3,    4};

[ mandatory ] name Space use the Camel name Method .

Example:

Equipments.heavyweapons = {};

[ mandatory ] An abbreviation consisting of multiple words, in which the capitalization of all letters is consistent with the case of the first letter, depending on the current naming method and where it appears.

Example:

function Xmlparser () {} function inserthtml (element, HTML) {} var New HttpRequest ();

[ mandatory ] class name use nouns .

Example:

function Engine (options) {}

[ recommended ] Name of function use the verb phrase .

Example:

function GetStyle (Element) {}

[ recommended ] Boolean  variables of type use is or has start.

Example:

var false ; var false;

2 language Features

2.1 variables

[ mandatory ] variables must be passed before they are used var definition.

Explain:

Defining a variable without var will cause the variable to contaminate the global environment.

Example:

// Good var name = ' MyName '; //  Bad  = ' MyName ';

2.2 Object Oriented

[ mandatory ] Create objects using the constructor pattern and the prototype pattern combination.

Example:

function  Person (name, age) {  this. Name = name;  this. Age =function  () {    alert (this. name);}; var New Person (' Tom '); var New Person (' Greg ',);p erson1.sayname (); alert (person2.age);

[ mandatory ] The inheritance of classes uses parasitic combined inheritance.

Example:

functionInheritprototype (subtype, supertype) {functionF () {} F.prototype=Supertype.prototype; varPrototype =NewF (); Prototype.constructor=subtype; Subtype.prototype=prototype;}functionsupertype (name) { This. Name =name;  This. colors = [' Red ', ' blue ', ' green '];} SuperType.prototype.sayName=function() {Console.log ( This. name);};functionsubtype (name, age) {Supertype.call ( This, name);  This. Age =Age ;}
Inheritprototype (subtype, supertype); SubType.prototype.sayAge=function() {Console.log ( This. age);};varInstance1 =NewSubtype (' Nicholas ', 29); Instance1.colors.push (' Black '); Console.log (instance1.colors); Instance1.sayname (); Instance1.sayage () ;varInstance2 =NewSubtype (' Greg ', 27); Console.log (instance2.colors); Instance2.sayname (); Instance2.sayage () ;

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.