【进阶篇】JavaScript代码规范

简介: 【进阶篇】JavaScript代码规范

 易读、易复用、易重构的 JavaScript 代码规范

1.变量命名规范有意义

Bad:
const yyyymmdstr = moment().format("YYYY/MM/DD");
Good:
const currentDate = moment().format("YYYY/MM/DD");

image.gif

2. 给变量定义名字

Bad:
// What the heck is 86400000 for?
setTimeout(blastOff, 86400000);
Good:
// Declare them as capitalized named constants.
const MILLISECONDS_IN_A_DAY = 86_400_000;
setTimeout(blastOff, MILLISECONDS_IN_A_DAY);

image.gif

3.函数的变量定义初始值

Bad:
function createMicrobrewery(name) {
  const breweryName = name || "Hipster Brew Co.";
  // ...
}
Good:
function createMicrobrewery(name = "Hipster Brew Co.") {
  // ...
}

image.gif

4.函数的形参过多时候,用对象代替多个参数

Bad:
function createMenu(title, body, buttonText, cancellable) {
  // ...
}
createMenu("Foo", "Bar", "Baz", true);
Good:
function createMenu({ title, body, buttonText, cancellable }) {
  // ...
}
createMenu({
  title: "Foo",
  body: "Bar",
  buttonText: "Baz",
  cancellable: true
});

image.gif

5.函数应该只做一件事情

Bad:
function emailClients(clients) {
  clients.forEach(client => {
    const clientRecord = database.lookup(client);
    if (clientRecord.isActive()) {
      email(client);
    }
  });
}
Good:
function emailActiveClients(clients) {
  clients.filter(isActiveClient).forEach(email);
}
function isActiveClient(client) {
  const clientRecord = database.lookup(client);
  return clientRecord.isActive();
}

image.gif

6.函数名称应该说明其作用

Bad:
function addToDate(date, month) {
  // ...
}
const date = new Date();
// It's hard to tell from the function name what is added
addToDate(date, 1);
Good:
function addMonthToDate(month, date) {
  // ...
}
const date = new Date();
addMonthToDate(1, date)

image.gif

7.使用Object.assign拷贝对象

Good:
const menuConfig = {
  title: "Order",
  // User did not include 'body' key
  buttonText: "Send",
  cancellable: true
};
function createMenu(config) {
  config = Object.assign(
    {
      title: "Foo",
      body: "Bar",
      buttonText: "Baz",
      cancellable: true
    },
    config
  );
  // config now equals: {title: "Order", body: "Bar", buttonText: "Send", cancellable: true}
  // ...
}
createMenu(menuConfig);

image.gif

8.开闭原则

   对扩展开放,对修改关闭;

  面向对象开发

9.函数的继承

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() {
    /* ... */
  }
}

image.gif

10  链式编程 在每个函数的结尾返回this即可

(这种模式在JavaScript中非常有用,您可以在jQuery和Lodash等许多库中看到它。它使您的代码更具表现力,并且不再那么冗长。出于这个原因,我说,使用方法链接并查看代码的干净程度。在类函数中,只需this在每个函数的末尾返回即可,然后可以将更多的类方法链接到该函数上。)

class Car {
  constructor(make, model, color) {
    this.make = make;
    this.model = model;
    this.color = color;
  }
  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("Ford", "F-150", "red").setColor("pink").save();

image.gif

11.async/await 来处理异步编程

参考地址来源 :GitHub - ryanmcdermott/clean-code-javascript: Clean Code concepts adapted for JavaScript


目录
相关文章
|
Linux 网络安全 开发工具
python更换版本
python更换版本
961 1
|
SQL 关系型数据库 MySQL
TiDB安装简介
TiDB安装简介
3246 0
VOS3000 AXB功能使用说明
VOS AXB 模块适用于语音市场直连运营商或虚拟运营商 X 号平台的业务需求 与 VOS 系统无缝集成,无需独立服务器部署,节约硬件,网络成本 单机支持不低于 2,000 并发 AXB 呼叫,性能是市面常见 AXB 产品的 2-3 倍 支持设定在呼叫接通后任意时间提前解绑,释放 X 号码资源,无需等到呼叫挂断后释放,大幅提高 X 号码利用率 提供完备的 AXB 计费策略与报表输出,兼容全类型运营商小号接口,保障业务能够平稳开展,持久运营
2115 0
|
搜索推荐 安全 Linux
Visual Studio Code 提高效率的快捷方式(下)
Visual Studio Code 是目前最流行、最常用的代码编辑器之一,它是开源的并且可以免费使用。它还提供对多种语言和框架的支持。 ​ 注意,这些 Visual Studio Code 快捷方式取自于 VS Code 官方文档。如果某些快捷方式不起作用,可能是由于编辑器或文件格式中的快捷方式发生了变化,或者安装的扩展影响了该快捷方式。
1845 0
|
存储 缓存 安全
【C/C++ 项目优化实战】 分享几种基础且高效的策略优化和提升代码性能
【C/C++ 项目优化实战】 分享几种基础且高效的策略优化和提升代码性能
1236 0
|
JavaScript C# 数据安全/隐私保护
C# 软件Licence应用实例
我们在使用一些需要购买版权的软件产品时,或者我们做的商业软件需要进行售卖,为了收取费用,一般需要一个软件使用许可证,然后输入这个许可到软件里就能够使用软件。简单的是一串序列码或者一个许可证文件,复杂的是一个定制化插件包。于是有的小伙伴就开始好奇这个许可是怎么实现的,特别是在离线情况下它是怎么给软件授权,同时又能避免被破解的。
1126 0
|
前端开发 Java Spring
SpringBoot2 | SpringBoot Environment源码分析(四)
SpringBoot2 | SpringBoot Environment源码分析(四)
392 0
|
Linux Docker Python
Python实战技术 - Python虚拟隔离环境 和 Docker技术
Python实战技术 - Python虚拟隔离环境 和 Docker技术
1463 0
|
机器学习/深度学习
【DR_CAN-MPC学习笔记】2.最优化数学建模推导
【DR_CAN-MPC学习笔记】2.最优化数学建模推导
|
Web App开发 存储 安全
浏览器跨域的配置
浏览器跨域的配置
1218 0
浏览器跨域的配置

热门文章

最新文章