Skip to content

Latest commit

 

History

History
421 lines (349 loc) · 7.27 KB

JavaScript.md

File metadata and controls

421 lines (349 loc) · 7.27 KB

JavaScript Code Style Guide

  1. 基本编码风格
  2. Objects
  3. Arrays
  4. Functions
  5. Class
  6. jQuery

1 基本编码风格

1.1 缩进

建议 使用一个Tab进行缩进,根据自己的编码习惯设置IDE/Text Editor indent => tabsize = 4/2 whitespace,这样无论是2空格缩进还是4空格都能保持一致 #2

// bad
function foo() {
  console.log('test');
}

// good
function foo() {
    console.log('test');
}

⬆ back to top

1.2 空格与换行

强制 二元运算符两侧必须要有一个空格,一元运算符与操作对象之间不应该有空格。

// bad
var z=x+y;
var a = ! z;

// good
var z = x + y;
var a = !z;

强制 块代码{之前必须要有一个空格。

// bad
if(condition){}

// good
if (condition) {}

强制if / else / for / while / function / switch / do / try / catch / finally 关键字后,必须有一个空格。

// bad
function(){}
while(true){}

// good
function () {}
while (true) {}

强制 在对象创建时,属性中的 : 之后必须有空格,: 之前不允许有空格。

// bad
var obj = {
    a : 1,
    b:2,
    c :3
};

// good
var obj = {
    a: 1,
    b: 2,
    c: 3
};

建议 非匿名函数声明及调用函数名与()之前不应该存在空格。

// bad
function foo () {}
foo ();

// good
function foo() {}
foo();

强制 函数参数之间由逗号空格隔开。

// bad
function foo(a,b,c) {}
foo(a,b,c);

// good
function foo(a, b, c) {}
foo(a, b, c);

建议 功能逻辑不同的代码块应该换行,提高阅读性。

// bad
function getDataset(el) {
    if (el === undefined) {
        return;
    }
    // logic
}

// good
function getDataset(el) {
    if (el === undefined) {
        return;
    }

    // logic
}

强制if / else / for / do / while 语句中,即使只有一行,也不得省略块 {...}。

// bad
if (condition) return;

// good
if (condition) {
    return;
}

⬆ back to top

1.3 命名

强制 变量、函数使用camelCase

// bad
var usertoken;
var function = getusertoken() {};

// good
var userToken;
var function = getUserToken() {};

强制 类、组件使用PascalCase命名。

// bad
var myCard = function(name) {
    this.name = name;
}

// good
var MyCard = function(name) {
    this.name = name;
}

强制 枚举变量使用PascalCase,属性使用全大写。

// bad
var testState = {
    a: 1,
    b: 2
};

// good
var TestState = {
    A: 1,
    B: 2
};

⬆ back to top

1.4 分号

强制 所有语句、函数表达式后面必须加分号,IIFE(immediately invoked function expressions)格式模块前后必须加一个分号#1

// bad
var a = 0
var foo = function() {}
(function() {})()

// good
var a = 0;
var foo = function() {};
;(function() {})();

⬆ back to top

1.5 逗号

建议 数组元素或者对象属性最后一个不要多引入一个逗号。

// bad
var obj = {
    a: 1,
    b: 2,
};
var arr = [1, 2, 3,];

// good
var obj = {
    a: 1,
    b: 2
};
var arr = [1, 2, 3];

⬆ back to top

1.6 引号

强制 字符串类型值使用单引号。

// bad
var str = "test";
var arr = ["test0", "test1"];
var obj = {
    a: "test",
    b: "test"
};

// good
var str = 'test';
var arr = ['test0', 'test1'];
var obj = {
    a: 'test',
    b: 'test'
};

⬆ back to top

1.7 变量

强制 在模块中使用var来声明一个非全局变量,全局变量请挂在在window对象上。

// bad
a = 1;

// good
var a = 1;
window.b = 2;

建议 都使用var来声明一个变量#3

// bad
var name, age, email;

// bad
var name,
    age,
    email;

// good
var name;
var age;
var email;

⬆ back to top

1.8 比较操作

强制 使用===``!==来进行比较而不是==``!=

// bad
var x = '0';
x == 0; // => true

// good
var x = '0';
x === 0; // => true

⬆ back to top

2 Objects

建议 使用字面意义的方式来创建对象。

// bad
var obj = new Object();

// good
var obj = {};

⬆ back to top

3 Arrays

建议 使用字面意义的方式创建数组。

// bad
var arr = new Array();

// good
var arr = [];

建议 使用slice方法拷贝数组。

var len = items.length;
var itemsCopy = [];
var i;

// bad
for (i = 0; i < len; i++) {
    itemsCopy[i] = items[i];
}

// good
itemsCopy = items.slice();

建议 添加数组元素使用push方法。

// bad
arr[arr.length] = 'test';

// good
arr.push('test');

建议 使用slice方法将一个类数组对象转换为数组。

function foo() {
    var args = Array.prototype.slice.call(arguments);
}

⬆ back to top

4 Functions

强制 不要在if语句中声明一个函数,应该使用函数表达式。

// bad
if (flag) {
    function foo() {}
}

// good
if (flag) {
    var foo = function foo() {};
}

建议 函数表达式后面跟上函数名,这会有利于调试bug。

// bad
var foo = function() {};

// good
var foo = function foo() {};

⬆ back to top

5 Class

建议 不要使用下划线来标识一个私有成员方法或者成员属性,JavaScript中根本不存在私有成员的概览,实际上他们都是public的。

// bad
function Person() {
    this._name; // => public
    this._getName = function() {}; // => public
}

// good
function Person() {
    this.name; // => public
    this.getName = function() {}; // => public
}

⬆ back to top

6 jQuery

强制 jQuery对象前加$进行标识。

// bad
var body = $('body');

// good
var $body = $('body');

强制 缓存jQuery对象。

// bad
$('.container').addClass('test');
$('.container').css({'background-color': 'pink'});
$('.container').hide();

// good
var $container = $('.container');
$container.addClass('test').css({'background-color': 'pink'}).hide();

⬆ back to top


参考