谷歌js規(guī)范(1)

-聲明常量、不能改變的量 用const:NAMES_LIKE_THIS(大寫字母+下劃線的組合)
-js語句是以分號作為結(jié)束。注意:第一種函數(shù)表達(dá)式應(yīng)當(dāng)有分號,第二種函數(shù)聲明沒有分號。

var foo = function() {
  return true;
};  // semicolon here.
function foo() {
  return true;
}  // no semicolon here.

-函數(shù)聲明不能在塊里,函數(shù)表達(dá)式的形式聲明可以。第一種不可,第二種可以。

if (x) {
  function foo() {}
}
if (x) {
  var foo = function() {};
}

-能用聲明原始類型如:string,number解決的事兒不要聲明一個對象

var x = new Boolean(false);
typeof new Boolean(0) == 'object';
var x = Boolean(0);
typeof Boolean(0) == 'boolean';

-避免delete,采用賦值為null的方式

Foo.prototype.dispose = function() {
  this.property_ = null;
};
Foo.prototype.dispose = function() {
  delete this.property_;
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容