您的位置:首页 > Web前端 > JavaScript

Javascript 风格向导(终结)

2013-04-24 15:10 288 查看


  继续前两篇,这篇作为终结篇。

Blocks

• 有{}的代码,我们换行处理。

// bad
if (test)
return false;

// good
if (test) return false;

// good
if (test) {
return false;
}

// bad
function() { return false; }

// good
function() {
return false;
}


Comments

• 对于多行注释使用/** ... */。包含描述信息、参数类型和返回值。

// bad
// make() returns a new element
// based on the passed in tag name
//
// @param <String> tag
// @return <Element> element
function make(tag) {

// ...stuff...

return element;
}

// good
/**
* make() returns a new element
* based on the passed in tag name
*
* @param <String> tag
* @return <Element> element
*/
function make(tag) {

// ...stuff...

return element;
}


• 对于单行注释使用//。单行注释单独放置在一个新行上。在注释前面放置一个空行。

// bad
var active = true;  // is current tab

// good
// is current tab
var active = true;

// bad
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
var type = this._type || 'no type';

return type;
}

// 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: 注释问题

function Calculator() {

// FIXME: shouldn't use a global here
total = 0;

return this;
}


• 使用 // TODO: 注释问题的解决方案

function Calculator() {

// TODO: total should be configurable by an options param
this.total = 0;

return this;
}


Type Casting & Coercion

• 在声明之前执行强制类型转换。
• 字符串

//  => this.reviewScore = 9;

// bad
var totalScore = this.reviewScore + '';

// good
var totalScore = '' + this.reviewScore;

// bad
var totalScore = '' + this.reviewScore + ' total score';

// good
var totalScore = this.reviewScore + ' total score';


• 对于数字转换,使用parseInt,而且要带着类型转换的基数。

• 如果parseInt成为你的瓶颈,处于性能原因,需要你使用“位移”操作。那么请写下注释解释你这样做的原因。

var inputValue = '4';

// bad
var val = new Number(inputValue);

// bad
var val = +inputValue;

// bad
var val = inputValue >> 0;

// bad
var val = parseInt(inputValue);

// good
var val = Number(inputValue);

// good
var val = parseInt(inputValue, 10);

// good
/**
* parseInt 使我的代码变慢.
* 为了提高速度,使用位移操作让字符串强制转化为数字。
*/
var val = inputValue >> 0;


• 布尔

var age = 0;

// bad
var hasAge = new Boolean(age);

// good
var hasAge = Boolean(age);

// good
var hasAge = !!age;


Constructors

• 用方法扩展对象,而不是用一个新对象。

function Jedi() {
console.log('new jedi');
}

// bad
Jedi.prototype = {
fight: function fight() {
console.log('fighting');
},

block: function block() {
console.log('blocking');
}
};

// good
Jedi.prototype.fight = function fight() {
console.log('fighting');
};

Jedi.prototype.block = function block() {
console.log('blocking');
};


• 让对象的方法return this,这样有利于方法的链锁操作。

// bad
Jedi.prototype.jump = function() {
this.jumping = true;
return true;
};

Jedi.prototype.setHeight = function(height) {
this.height = height;
};

var luke = new Jedi();
luke.jump(); // => true
luke.setHeight(20) // => undefined

// good
Jedi.prototype.jump = function() {
this.jumping = true;
return this;
};

Jedi.prototype.setHeight = function(height) {
this.height = height;
return this;
};

var luke = new Jedi();

luke.jump()
.setHeight(20);


• 我们可以自定义一个toString()方法。——要确保它能正常运行,而且不会产生其他影响。

function Jedi(options) {
options || (options = {});
this.name = options.name || 'no name';
}

Jedi.prototype.getName = function getName() {
return this.name;
};

Jedi.prototype.toString = function toString() {
return 'Jedi - ' + this.getName();
};


总结

  终于算是写完了,希望能够对大家所有帮助。

推荐

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: