javascript的原則

背景

這篇算是javascript的原則第二篇把蛔溃,前一篇主要基礎(chǔ)的講解,這篇主要會講一些數(shù)據(jù)結(jié)構(gòu)和異常處理的講解。

對象和數(shù)據(jù)結(jié)構(gòu)

JavaScript 沒有接口或類型秒梳, 所以堅持這個模式是非常困難的法绵, 因為我們沒有 public 和 private 關(guān)鍵字。 正因為如此酪碘, 使用 getters 和 setters 來訪問對象上的數(shù)據(jù)比簡單的在一個對象上查找屬性 要好得多朋譬。 “為什么?” 你可能會問兴垦, 好吧徙赢, 原因請看下面的列表:

  • 當(dāng)你想在獲取一個對象屬性的背后做更多的事情時, 你不需要在代碼庫中查找和修改每一處訪問探越;
    使用 set 可以讓添加驗證變得容易狡赐;
    封裝內(nèi)部實現(xiàn);
    使用 getting 和 setting 時钦幔, 容易添加日志和錯誤處理枕屉;
    繼承這個類, 你可以重寫默認(rèn)功能鲤氢;
    你可以延遲加載對象的屬性搀擂, 比如說從服務(wù)器獲取。
  • 使用 set 可以讓添加驗證變得容易铜异;
  • 封裝內(nèi)部實現(xiàn)
  • 使用 getting 和 setting 時哥倔, 容易添加日志和錯誤處理;
  • 繼承這個類揍庄, 你可以重寫默認(rèn)功能咆蒿;
  • 你可以延遲加載對象的屬性, 比如說從服務(wù)器獲取蚂子。
Bad:class BankAccount {
  constructor() {
    this.balance = 1000;
  }
}
const bankAccount = new BankAccount();
// Buy shoes...
bankAccount.balance -= 100;

Goods:
>class BankAccount {
  constructor(balance = 1000) {
    this._balance = balance;
  }
  // It doesn't have to be prefixed with `get` or `set` to be a getter/setter
  set balance(amount) {
    if (verifyIfAmountCanBeSetted(amount)) {
      this._balance = amount;
    }
  }
  get balance() {
    return this._balance;
  }
verifyIfAmountCanBeSetted(val) {
    // ...
  }
}
const bankAccount = new BankAccount();
// Buy shoes...
bankAccount.balance -= shoesPrice;
// Get balance
let balance = bankAccount.balance;

私有變量

Bad:
>const Employee = function(name) {
  this.name = name;
};
Employee.prototype.getName = function getName() {
  return this.name;
};
const employee = new Employee('John Doe');
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
delete employee.name;
console.log(`Employee name: ${employee.getName()}`); // Employee name: undefined

Good:
>const Employee = function (name) {
  this.getName = function getName() {
    return name;
  };
};
const employee = new Employee('John Doe');
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
delete employee.name;
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe

ES2015/ES6 類優(yōu)先與 ES5

Bad:
> const Animal = function(age) {
  if (!(this instanceof Animal)) {
    throw new Error('Instantiate Animal with `new`');
  }
  this.age = age;
};
Animal.prototype.move = function move() {};
const Mammal = function(age, furColor) {
  if (!(this instanceof Mammal)) {
    throw new Error('Instantiate Mammal with `new`');
  }
  Animal.call(this, age);
  this.furColor = furColor;
};
Mammal.prototype = Object.create(Animal.prototype);
Mammal.prototype.constructor = Mammal;
Mammal.prototype.liveBirth = function liveBirth() {};
const Human = function(age, furColor, languageSpoken) {
  if (!(this instanceof Human)) {
    throw new Error('Instantiate Human with `new`');
  }
  Mammal.call(this, age, furColor);
  this.languageSpoken = languageSpoken;
};
Human.prototype = Object.create(Mammal.prototype);
Human.prototype.constructor = Human;
Human.prototype.speak = function speak() {};

Good:
>class Animal {
  constructor(age) {
    this.age = age;
  }
  move() { /* ... */ }
}
class Mammal extends Animal {
  constructor(age, furColor) {
    super(age);
    this.furColor = furColor;
  }
  liveBirth() { /* ... */ }
}
class Human extends Mammal {
  constructor(age, furColor, languageSpoken) {
    super(age, furColor);
    this.languageSpoken = languageSpoken;
  }
  speak() { /* ... */ }
}

Class 支持方法鏈

Bad:
>class Car {
  constructor() {
    this.make = 'Honda';
    this.model = 'Accord';
    this.color = 'white';
  }
  setMake(make) {
    this.make = make;
  }
  setModel(model) {
    this.model = model;
  }
  setColor(color) {
    this.color = color;
  }
  save() {
    console.log(this.make, this.model, this.color);
  }
}
const car = new Car();
car.setColor('pink');
car.setMake('Ford');
car.setModel('F-150');
car.save();

Good:
>class Car {
  constructor() {
    this.make = 'Honda';
    this.model = 'Accord';
    this.color = 'white';
  }
  setMake(make) {
    this.make = make;
    // NOTE: Returning this for chaining
    return this;
  }
  setModel(model) {
    this.model = model;
    // NOTE: Returning this for chaining
    return this;
  }
  setColor(color) {
    this.color = color;
    // NOTE: Returning this for chaining
    return this;
  }
  save() {
    console.log(this.make, this.model, this.color);
    // NOTE: Returning this for chaining
    return this;
  }
}
const car = new Car()
  .setColor('pink')
  .setMake('Ford')
  .setModel('F-150')
  .save();

單一職責(zé)原則 (SRP)

正如代碼整潔之道所述沃测, “永遠不要有超過一個理由來修改一個類”。

Bad:
>class UserSettings {
  constructor(user) {
    this.user = user;
  }
  changeSettings(settings) {
    if (this.verifyCredentials()) {
      // ...
    }
  }
  verifyCredentials() { }
}

Good:
>class UserAuth {
  constructor(user) {
    this.user = user;
  }
  verifyCredentials() {
    // ...
  }
}
class UserSettings {
  constructor(user) {
    this.user = user;
    this.auth = new UserAuth(user);
  }
  changeSettings(settings) {
    if (this.auth.verifyCredentials()) {
      // ...
    }
  }
}

并發(fā)

使用 Promises, 不要使用回調(diào)

Bad:
>require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', (requestErr, response) => {
  if (requestErr) {
    console.error(requestErr);
  } else {
    require('fs').writeFile('article.html', response.body, (writeErr) => {
      if (writeErr) {
        console.error(writeErr);
      } else {
        console.log('File written');
      }
    });
  }
});

Good:
>require('request-promise').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
  .then((response) => {
    return require('fs-promise').writeFile('article.html', response);
  })
  .then(() => {
    console.log('File written');
  })
  .catch((err) => {
    console.error(err);
  });

Async/Await 比 Promises 更加簡潔

Promises 是回調(diào)的一個非常簡潔的替代品食茎, 但是 ES2017/ES8 帶來的 async 和 await 提供了一個 更加簡潔的解決方案蒂破。 你需要的只是一個前綴為 async 關(guān)鍵字的函數(shù), 接下來就可以不需要 then 函數(shù)鏈來編寫邏輯了别渔。

Bad:
>require('request-promise').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin')
  .then((response) => {
    return require('fs-promise').writeFile('article.html', response);
  })
  .then(() => {
    console.log('File written');
  })
  .catch((err) => {
    console.error(err);
  });

Good:
>async function getCleanCodeArticle() {
  try {
    const response = await require('request-promise').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin');
    await require('fs-promise').writeFile('article.html', response);
    console.log('File written');
  } catch(err) {
    console.error(err);
  }
}

格式化

使用一致的大小寫
JavaScript 是無類型的附迷, 所以大小寫告訴你關(guān)于你的變量、 函數(shù)等的很多事情哎媚。 這些規(guī)則是主觀的喇伯, 所以你的團隊可以選擇他們想要的。 重點是拨与, 不管你們選擇了什么稻据, 要保持一致。

Bad:
>const DAYS_IN_WEEK = 7;
const daysInMonth = 30;
const songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
const Artists = ['ACDC', 'Led Zeppelin', 'The Beatles'];
function eraseDatabase() {}
function restore_database() {}
class animal {}
class Alpaca {}

Good:
>const DAYS_IN_WEEK = 7;
const DAYS_IN_MONTH = 30;
const songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
const artists = ['ACDC', 'Led Zeppelin', 'The Beatles'];
function eraseDatabase() {}
function restoreDatabase() {}
class Animal {}
class Alpaca {}

注釋

代碼能夠說清的盡量不要寫注釋

Bad:
>function hashIt(data) {
  // The hash
  let hash = 0;
  // Length of string
  const length = data.length;
  // Loop through every character in data
  for (let i = 0; i < length; i++) {
    // Get character code.
    const char = data.charCodeAt(i);
    // Make the hash
    hash = ((hash << 5) - hash) + char;
    // Convert to 32-bit integer
    hash &= hash;
  }
}

Good:
>function hashIt(data) {
  let hash = 0;
  const length = data.length;
  for (let i = 0; i < length; i++) {
    const char = data.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    // Convert to 32-bit integer
    hash &= hash;
  }
}

不要有日志式的評論

要用版本控制來查看評論

Bad:
>/**  2016-12-20: Removed monads, didn't understand them (RM)
  2016-10-01: Improved using special monads (JP)
  2016-02-03: Removed type-checking (LI)
  2015-03-14: Added combine with type-checking (JR)
 */
function combine(a, b) {
  return a + b;
}

Good:
>function combine(a, b) {
  return a + b;
}

千里之行买喧,始于足下捻悯。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末匆赃,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子今缚,更是在濱河造成了極大的恐慌算柳,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荚斯,死亡現(xiàn)場離奇詭異埠居,居然都是意外死亡,警方通過查閱死者的電腦和手機事期,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來纸颜,“玉大人兽泣,你說我怎么就攤上這事⌒菜铮” “怎么了唠倦?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長涮较。 經(jīng)常有香客問我稠鼻,道長,這世上最難降的妖魔是什么狂票? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任候齿,我火速辦了婚禮,結(jié)果婚禮上闺属,老公的妹妹穿的比我還像新娘慌盯。我一直安慰自己,他們只是感情好掂器,可當(dāng)我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布亚皂。 她就那樣靜靜地躺著,像睡著了一般国瓮。 火紅的嫁衣襯著肌膚如雪灭必。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天乃摹,我揣著相機與錄音禁漓,去河邊找鬼。 笑死峡懈,一個胖子當(dāng)著我的面吹牛璃饱,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播肪康,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼荚恶,長吁一口氣:“原來是場噩夢啊……” “哼撩穿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谒撼,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤食寡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后廓潜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抵皱,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年辩蛋,在試婚紗的時候發(fā)現(xiàn)自己被綠了呻畸。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡悼院,死狀恐怖伤为,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情据途,我是刑警寧澤绞愚,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站颖医,受9級特大地震影響位衩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜熔萧,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一糖驴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧哪痰,春花似錦遂赠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至肋演,卻和暖如春抑诸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背爹殊。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工蜕乡, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人梗夸。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓层玲,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子辛块,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,647評論 2 354

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

  • 寫在開頭 最近在看原則一本書畔派,發(fā)現(xiàn)生活中和工作中很多都是有規(guī)律有原則的,代碼的世界里也一樣润绵,代碼其實是寫給人看的线椰,...
    i5yue閱讀 723評論 0 0
  • (a fork of Airbnb's Javascript Style Guide) Strikingly ES...
    飄零_zyw閱讀 1,161評論 1 2
  • 用更合理的方式寫 JavaScript 目錄 聲明變量 對象 數(shù)組 字符串 函數(shù) 箭頭函數(shù) 模塊 迭代器和生成器 ...
    小紅依閱讀 1,795評論 0 5
  • 姓名:3D打印創(chuàng)客教育名片張藝楠 樣子:四葉草形狀 顏色:漂亮的淡藍色(只有一臺哦) 輕重:非常非常的輕 底板厚度...
    時光的漏斗wp閱讀 317評論 0 1
  • 感謝阿里云9.9服務(wù)器學(xué)生計劃,讓我有機會實現(xiàn)自己小時候的夢想尘盼。為了能夠?qū)懗鲞@篇教程憨愉,我就默默把服務(wù)器(Ubunt...
    xuzhougeng閱讀 2,759評論 0 7