/*
1.类型types
原始值:存取直接作用于它自身
string
number
boolean
null
undefined
var foo=1;
var bar=foo;
bar=9;
console.log(foo,bar);//=>1,9
复杂类型:存取时作用于它自身值的引用
object
array
function
var foo=[1,2];
var bar=foo;
bar[0]=9;
console.log(foo,bar);//=>1,9
2.对象objects
使用直接量创建对象
//bad
var item=new Object();
//good
var item={};
不要使用保留字作为键名,它们在IE8下不工作
//bad
var superman={
default:{clark:‘kent’},
private:ture;
};
//good
var superman={
default:{clark:‘kent’},
hidden:ture;
};
使用同义词替换需要使用的保留字
//bad
var superman={
class:'alien'
};
//bad
var superman={
klass:'alien'
};
//good
var superman={
type:'alien'
};
3.数组arrays
使用直接量创建数组
//bad
var items=new Array();
//good
var items=[];
向数组增加元素时使用Array#push来替代直接赋值
var someStack=[];
//bad
someStack[someStack.length]='abcdefghi';
//good
someStack.push('abcdefghi');
当你需要拷贝数组时,使用Array#slice.
var len=items.length;
var itemsCopy=[];
var i;
//bad
for(i=0;i<len;i++){
itemsCopy[i]=items[i];
}
//good
itemsCopy=items.slice();
使用Array#slice将类数组对象转换成数组
function trigger(){
var args=Array.prototype.slice.call(arguments);
}
4.字符串strings
使用单引号‘’包裹字符串
//bad
var name=“bob parr";
//good
var name='bob parr';
//bad
var fullName="bob"+this.lastName;
//good
var fullName=‘bob’+this.lastName;
超过100个字符的字符串应该使用链接字符写成多行。
注:若过度使用,通过连接符连接的长字符串可能会影响性能。
//bad
var errorMessage='this is a super long error that was thrown because of batman.when you stip to think';
var errorMessage='this is a super long error that was thrown\
because of batman.\
when you stip to think';
//good
errorMessage='this is a super long error that was thrown'+
'because of batman.'+
'when you stip to think';
程序化生成的字符串使用Array#join连接而不是使用连接符。尤其是IE下;
var items;
var messages;
var length;
var i; messages=[{
state:'success',
message:'this one worked'
},{
state:'success',
message:'this one worked as well'
},{
state:'error',
message:'this one did not work'
}];
legnth=messages.length;
//bad
function inbox(messages){
items='<ul>';
for(i=0;i<length;i++){
items+='<li>'+messages[i].message+'</li>';
}
return items+'</ul>';
}
//good
function inbox(messages){
items=[];
for(i=0;i<length;i++){
items[i]='<li>'+messages[i].message+'</li>';
}
return '<ul>'+items.join('')+'</ul>';
}
5.函数functions
函数表达式
//匿名函数表达式
var anonymous=function(){
return true;
}
//命名函数表达式
var named=function named(){
return true;
}
//立即调用的函数表达式(IIFE)
(function(){
console.log('welcome to the internet');
}());
永远不要在一个非函数代码块(if、while等)中声明一个函数,把那个函数赋给一个变狼。浏览器允许你这么做,但它们的解析表现不一致。
注:ECMA-262把块定义为一组语句。函数声明不是语句。
//bad
if(currentUser){
function test(){
console.log('Nope');
}
}
//good
var test;
if(currentUser){
test=function test(){
console.log('Yup');
};
}
永远不要把参数命名为arguments。这将取代函数作用域内的arguments对象。
// bad
function nope(name, options, arguments) {
// ...stuff...
} // good
function yup(name, options, args) {
// ...stuff...
} 6.属性properties
使用.来访问对象的属性。
var luke={
jedi:ture;
age:28
};
//bad
var isJedi=luke['jedi'];
//good
var isJedi=luke.jedi;
当通过变量访问属性时,使用中括号[]
var luke={
jedi:ture;
age:28
};
function getProp(prop){
return luke[prop];
}
var isJedi=getProp('jedi');
7.变量varibles
总是使用var来声明变量。不这么做将导致产生全局变量。我们要避免污染全局命名空间。
//bad
superPower=new SuperPower();
//good
var superPower=new SuperPower();
使用var声明每一个变量。这样做的好处是增加新变量将变得更加容易,而且你永远不用再担心调换错;跟,。
//good
var items=getItems();
var goSportsTeam=ture;
var dr='z';
最后再声明未赋值的变量,当你需要引用前面的变量赋值时这将变的很有用。
var items=getItems();
var goSportsTeam=ture;
var dragonball;
var i;
在作用域顶部声明变量。这将帮助你避免变量声明提升相关的问题。
8.提升hoisting
变量声明会提升至作用域顶部,但赋值不会。
匿名函数表达式会提升它们的变量名,但不会提升函数的赋值。
命名函数表达式会提升函数名,但不会提升函数名或函数体。
函数声明提升它们的名字和函数体。
function example() {
superPower(); // => Flying function superPower() {
console.log('Flying');
}
}
9.比较预算法&等号comparison-operators-equality
优先使用===和!==而不是==和!=
条件表达式例如if语句通过抽象方法ToBoolean强制计算它们的表达式并且总是遵守下面的规则。
对象被计算为true
undefined被计算为false
Null被计算为false
布尔值被计算为布尔值
数字如果是+0、-0或NaN被计算为false,负责为true
字符串如果是空字符串‘’被计算为false,负责为true
if([0]){
//true
//一个数组就是一个对象,对象被计算为true
}
使用快捷方式
//bad
if(name!==''){
//stuff
}
//good
if(name){
//stuff
}
//bad
if(collection.length>0){
//stuff
}
//good
if(collection.length){
//stuff
}
10.块blocks
使用大括号包裹所有的多行代码块
if(test){
return false;
}
function(){
return false;
}
如果通过if和else使用多行代码块,把else放在if代码块关闭括号的同一行
if(test){
thing1();
}else{
thing2();
}
11.注释comments
使用/+*..*+/作为多行注释,包含描述、指定所有参数和返回值得类型和值
使用//作为单行注释,在评论对象上面另起一行使用单行注释。在注释钱插入空行
// good
// is current tab
var active = true; // good
function getType() {
console.log('fetching type...'); // set the default type to 'no type'
var type = this.type || 'no type'; return type;
}
使用FIXME或TODO的前缀可以帮助其他开发者快速了解这是一个需要复查的问题
//FIXME:shouldn`t use a global here
//TODO:total should be configurable by an options param
12.空白whitespace
使用2个空格作为缩进
在大括号前方一个空格
function test() { }
在控制语句(if、while等)的小括号前放一个空格。在函数调用及声明中,不在函数参数列表前加空格。
使用空格把运算符隔开。
var x = y + 5;
在文件末尾插入一个空行
在使用长方法链时进行缩进。使用前面的点.强调这是方法调用而不是新语句
$('items')
.find('selected')
.highlight()
.end()
.find('open')
.updateCount();
在块末和新语句前插入空行。
13.逗号commas
行首逗号:不需要
额外的行末逗号:不需要
var story=[
once,
upon,
aTime
];
14.分号semicolons
使用分号
//good
(function() {
var name='sky';
return name;
})();
//good (防止函数在两个IIFE合并时被当成一个参数)
;(function() {
var name='sky';
return name;
})();
15.类型转化type-casting-coercion
在语句开始时执行类型转换
使用parseInt转换数字时总是带上类型转换的基数;
var inputValue='4';
//bad
var val=new Number(inputValue);
//bad
var val=+inputValue;
//bad
var val=parseInt(inputValue);
//good
var val=Number(inputValue);
//good
var val=parseInt(inputValue,10);
布尔:
var age=0;
//bad
var hasAge=new Boolean(age);
//good
var hasAge=Boolean(age);
//good
var hasAge=!!age; 16.命名规则naming-conventions
避免单字母命名,命名应具备描述下
使用驼峰式命名对象、函数和实例
使用帕卡斯式命名构造函数或类
//bad
function user(){
this.namme=options.name;
}
//good
function User(){
this.namme=options.name;
}
var good=new User(){
name:'hip';
}
不要使用下划线前/后缀,javascript没有私有属性或方法的概念
//bad
this._firstName_='';
//good
this.firstName='';
不要保存this的应用,使用Function#bind
//bad
function(){
var self=this;
return function(){
console.log(self);
};
}
//good
function() {
return function() {
console.log(this);
}.bind(this);
}
给函数命名,这在做堆栈轨迹时很有帮助
//bad
var log=function(msg) {
console.log(msg);
};
//good
var log=function log(msg) {
console.log(msg);
} 17.存取器accessors
属性的存取函数不是必须的
如果你需要存取函数时使用getVal()和setVal('hello'),便于理解函数的用途
如果属性时布尔值,使用isVal()或hasVal()
18.事件events
当给事件附加数据时,传入一个哈希而不是原始值。这样可以让后面的贡献者增加更
多数据到事件数据而无需找出并更新事件的每一个处理器。
//bad
$(this).trigger('listingUpdated','listing.id');
//good
$(this).trigger('listingUpdated',{listingId:listing.id}); */

javascript-style-guide的更多相关文章

  1. Airbnb JavaScript Style Guide

      Airbnb JavaScript Style Guide() { 用更合理的方式写 JavaScript    ES5 的编码规范请查看版本一,版本二. 翻译自 Airbnb JavaScrip ...

  2. Google JavaScript Style Guide

    转自:http://google.github.io/styleguide/javascriptguide.xml Google JavaScript Style Guide Revision 2.9 ...

  3. electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google JavaScript Style Guide代码规范

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  4. JavaScript Style Guide中文总结

    github原址:https://github.com/airbnb/javascript 类型*基本类型:包括string.number.boolean.null.undefined,存储的是值本身 ...

  5. Google coding Style Guide : Google 编码风格/代码风格 手册/指南

    1 1 1 https://github.com/google/styleguide Google 编码风格/代码风格 手册/指南 Style guides for Google-originated ...

  6. 使用tdcss.js轻松制作自己的style guide

    http://jakobloekke.github.io/tdcss.js/ 在前端开发中,如果能够有一个style guide对于设计来说就显得专业稳定,一致性.在上述链接中,有一个tdcss.js ...

  7. Google HTML/CSS Style Guide

    转自: http://google.github.io/styleguide/htmlcssguide.xml Google HTML/CSS Style Guide Revision 2.23 Ea ...

  8. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  9. 与你相遇好幸运,The Moe Node.js Code Style Guide

    The Moe Node.js Code Style Guide  By 一个最萌的开发者 @2016.9.21 >>代码是人来阅读的,格式规范的代码是对编程人员最好的礼物 :) > ...

  10. Google C++ Style Guide在C++11普及后的变化

    转 http://www.cnblogs.com/chen3feng/p/5972967.html?from=timeline&isappinstalled=0&lwfrom=user ...

随机推荐

  1. Android Service

    一.在MainAcitivity界面启动Service  : public class MyService extends Service intent = new Intent(MainActivi ...

  2. 记录一次bug解决过程:规范变量名称和mybatis的使用以及代码优化

    一.总结 Mybatis中当parameterType为基本数据类型的时候,统一采用_parameter来代替基本数据类型变量. Mybatis中resultMap返回一个对象,resultType返 ...

  3. Java编程里的类和对象

    像我们搞计算机这块的,都知道这么一件事,当前的计算机编程语言主要分为两大块,一为面向过程,二为面向对象.Java就是一门纯面向对象的语言.学习了一个月左右的Java,在下对于Java当中的类和对象有了 ...

  4. 每次新建项目出现appcompat_v7 解决方法

    ADT升级版本后每次新建项目出现appcompat_v7 , 解决方案如下 问题生成:

  5. HTML DOM总结

    MDN的定义 文档对象模型 (DOM) 是 HTML 和 XML 文档的编程接口.它给文档(结构树)提供了一个结构化的表述并且定义了一种方式—程序可以对结构树进行访问,以改变文档的结构,样式和内容. ...

  6. js对象和继承总结

    创建对象方式: [工厂模式]:无法解决对象识别问题 [构造函数模式]:每个方法都要在每个实例上创建一遍 [原型模式]:原型上属性为引用类型的问题,见例子 [组合模式]:解决上述问题 [动态原型模式]: ...

  7. 你知道JavaScript中的结果值是什么吗?

    你知道JavaScript中的每条语句.甚至表达式都有一个结果值吗? 当你在浏览器中测试代码时,经常会在控制台的输出结果的最后面多出一条,大部分为undefined,这个undefined就是一个结果 ...

  8. eclipse启动tomcat无法访问

    eclipse启动tomcat无法访问 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能 ...

  9. Linux安装xwindow图形界面(转载)

    http://jingyan.baidu.com/article/7f766daf42ce984100e1d045.html 1.检查Linux系统是否能够联网. 2.执行命令 yum -y grou ...

  10. django 1.10 CSRF验证失败的解决过程

    最近工作闲,没事自学django,感觉这个最烦的就是各版本提供的api函数经常有变化,不是取消了就是参数没有了,网上搜到的帖子也没说明用的是什么版本的django,所以经常出现搬运过来的代码解决不了问 ...