首页 > 编程语言 > 详细

JavaScript Patterns 2.10 Naming Conventions

时间:2014-05-25 20:15:41      阅读:353      评论:0      收藏:0      [点我收藏+]

1. Capitalizing Constructors

var adam = new Person();

2. Separating Words

camel case - type the words in lowercase, only capitalizing the first letter in each word.

upper camel case, as in  MyConstructor(),

lower  camel  case,  as  in  myFunction(), calculateArea()and getFirstName()

variable names - first_name,  favorite_bands,  and old_company_name.

 

ECMAScript uses camel case for both methods and properties, although the multiword property  names are rare (lastIndex and  ignoreCase properties of regular expression objects).

 

3. Other Naming Patterns

Constants - Number.MAX_VALUE

// precious constants, please don‘t touch

var PI = 3.14,

MAX_WIDTH = 800;

 

Naming globals with all caps can reinforce the practice of minimizing their number and can make them easily distinguishable.

 

use an underscore prefix to denote a private method or property.

bubuko.com,布布扣
var person = {

    getName: function () {
        return this._getFirst() + ‘ ‘ + this._getLast();
    },

    _getFirst: function () {
        // ...
    },

    _getLast: function () {
        // ...
    }
}; 
bubuko.com,布布扣

Note that JSLint will complain about the underscore prefixes, unless you set the option nomen: false.

 

Following are some varieties to the _private convention:

? Using a trailing underscore to mean private, as in name_ and getElements_()

? Using  one  underscore  prefix  for  _protected properties  and  two  for  __private properties

? In Firefox some internal properties not technically part of the language are available, and they are named with a two underscores prefix and a two underscore suffix, such as __proto__ and __parent__ 

JavaScript Patterns 2.10 Naming Conventions,布布扣,bubuko.com

JavaScript Patterns 2.10 Naming Conventions

原文:http://www.cnblogs.com/haokaibo/p/Naming-Conventions.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!