如何写出干净整洁的JavaScript

作者: 前端小咖 | 来源:发表于2019-11-07 18:48 被阅读0次

变量

使用有意义和可理解的变量名

bad:

constyyyymmdstr = moment().format("YYYY/MM/DD");复制代码

good:

constcurrentDate = moment().format("YYYY/MM/DD");复制代码

使用具有解释性的变量

Bad:

constaddress ="One Infinite Loop, Cupertino 95014";constcityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;saveCityZipCode(  address.match(cityZipCodeRegex)[1],  address.match(cityZipCodeRegex)[2]);复制代码

Good:

constaddress ="One Infinite Loop, Cupertino 95014";constcityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;const[, city, zipCode] = address.match(cityZipCodeRegex) || [];saveCityZipCode(city, zipCode);复制代码

避免使用隐式映射

Bad:

constlocations = ["Austin","New York","San Francisco"];locations.forEach(l => {  doStuff();  doSomeOtherStuff();// ...// ...// ...// Wait, what is `l` for again?dispatch(l);});复制代码

Good:

constlocations = ["Austin","New York","San Francisco"];locations.forEach(location => {  doStuff();  doSomeOtherStuff();// ...// ...// ...dispatch(location);});复制代码

不要添加不必要的内容

Bad:

constCar = {  carMake:"Honda",  carModel:"Accord",  carColor:"Blue"};functionpaintCar(car){  car.carColor ="Red";}复制代码

Good:

constCar = {  make:"Honda",  model:"Accord",  color:"Blue"};functionpaintCar(car){  car.color ="Red";}复制代码

函数

函数参数因少于等于两个

限制你的函数参数数量是非常有必要的,这可以让你更容易测试你的函数。理想情况下函数参数等于小于两个,如果超过了两个,只能证明你的函数做的太多了,违背了单一功能的设计原则。

Bad:

functioncreateMenu(title, body, buttonText, cancellable){// ...}复制代码

Good:

functioncreateMenu({title,body,buttonText,cancellable}) {// ...}createMenu({title:"Foo",body:"Bar",buttonText:"Baz",cancellable: true});复制代码

不要使用标志作为函数的参数

函数功能应该保持单一功能。

bad

functioncreateFile(name, temp){if(temp) {    fs.create(`./temp/${name}`);  }else{    fs.create(name);  }}复制代码

good

functioncreateFile(name){  fs.create(name);}functioncreateTempFile(name){  createFile(`./temp/${name}`);}复制代码

使用默认参数

Bad:

functioncreateMicrobrewery(name){constbreweryName = name ||"Hipster Brew Co.";// ...}复制代码

Good:

functioncreateMicrobrewery(name ="Hipster Brew Co."){// ...}复制代码

函数应该只处理同一抽象级别的功能

当你的函数要处理多个层级的功能,那你的函数就太复杂了。你应该把函数分割成各个层级以方便复用和测试。

Bad:

functionparseBetterJSAlternative(code){constREGEXES = [// ...];conststatements = code.split(" ");consttokens = [];  REGEXES.forEach(REGEX => {    statements.forEach(statement => {// ...});  });constast = [];  tokens.forEach(token => {// lex...});  ast.forEach(node => {// parse...});}复制代码

Good:

functionparseBetterJSAlternative(code){consttokens = tokenize(code);constsyntaxTree = parse(tokens);  syntaxTree.forEach(node => {// parse...});}functiontokenize(code){constREGEXES = [// ...];conststatements = code.split(" ");consttokens = [];  REGEXES.forEach(REGEX => {    statements.forEach(statement => {      tokens.push(/* ... */);    });  });returntokens;}functionparse(tokens){constsyntaxTree = [];  tokens.forEach(token => {    syntaxTree.push(/* ... */);  });returnsyntaxTree;}复制代码

避免副作用

一个函数会产生副作用除非它只读取参数然后返回另外一个参数

Bad:

// Global variable referenced by following function.// If we had another function that used this name, now it'd be an array and it could break it.letname ="Ryan McDermott";functionsplitIntoFirstAndLastName(){  name = name.split(" ");}splitIntoFirstAndLastName();console.log(name);// ['Ryan', 'McDermott'];复制代码

Good:

functionsplitIntoFirstAndLastName(name){returnname.split(" ");}constname ="Ryan McDermott";constnewName = splitIntoFirstAndLastName(name);console.log(name);// 'Ryan McDermott';console.log(newName);// ['Ryan', 'McDermott'];复制代码

Bad:

constaddItemToCart = (cart, item) => {  cart.push({ item,date:Date.now() });};复制代码

Good:

constaddItemToCart =(cart, item) =>{return[...cart, { item,date:Date.now() }];};复制代码

封装条件语句

Bad:

if(fsm.state ==="fetching"&& isEmpty(listNode)) {// ...}复制代码

Good:

functionshouldShowSpinner(fsm, listNode){returnfsm.state ==="fetching"&& isEmpty(listNode);}if(shouldShowSpinner(fsmInstance, listNodeInstance)) {// ...}复制代码

避免使用否定语句

Bad:

functionisDOMNodeNotPresent(node){// ...}if(!isDOMNodeNotPresent(node)) {// ...}复制代码

Good:

functionisDOMNodePresent(node){// ...}if(isDOMNodePresent(node)) {// ...}复制代码

对象和数据结构

使用setters跟getters

当你想做的不仅仅是获取一个对象属性,你不需要查找和更改代码库中的每个访问器。

使在执行集合时添加验证变得简单。

封装内部表示。

在获取和设置时易于添加日志记录和错误处理。

可以延迟加载对象的属性,例如从服务器获取。

Bad:

functionmakeBankAccount(){// ...return{    balance:0// ...};}constaccount = makeBankAccount();account.balance =100;复制代码

Good:

functionmakeBankAccount(){// this one is privateletbalance =0;// a "getter", made public via the returned object belowfunctiongetBalance(){returnbalance;  }// a "setter", made public via the returned object belowfunctionsetBalance(amount){// ... validate before updating the balancebalance = amount;  }return{// ...getBalance,    setBalance  };}constaccount = makeBankAccount();account.setBalance(100);复制代码

使用链式调用

Bad:

classCar{constructor(make, model, color) {this.make = make;this.model = model;this.color = color;  }  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("Ford","F-150","red");car.setColor("pink");car.save();复制代码

Good:

classCar{constructor(make, model, color) {this.make = make;this.model = model;this.color = color;  }  setMake(make) {this.make = make;//NOTE:Returning this for chainingreturnthis;  }  setModel(model) {this.model = model;//NOTE:Returning this for chainingreturnthis;  }  setColor(color) {this.color = color;//NOTE:Returning this for chainingreturnthis;  }  save() {    console.log(this.make,this.model,this.color);//NOTE:Returning this for chainingreturnthis;  }}const car = new Car("Ford","F-150","red").setColor("pink").save();复制代码

如果对于学习编程有很多疑惑,没有思路,不知道如何有效率的学习,可以添加我的前端交流学习群 895757445,需要最新系统的学习教程也可以管我要。做了很多年开发,对于学习方式,如何提高自己的技术有一定的经验,术业有专攻,多跟有经验的人交流学习,对这个行业信息了解的多,职业发展的空间就越大

相关文章

  • 如何写出干净整洁的JavaScript

    变量 使用有意义和可理解的变量名 bad: constyyyymmdstr = moment().format("...

  • 写出整洁的 JavaScript 代码

    前言 每个人写代码风格不一样,但本文给出了十个不同正反例说明,大家可以多参考,但不一定要遵守。本文由@aliveb...

  • 干净、整洁

    下班前李总(我们公司的一个供货商)送来一沓发票。他说,对一下,下周一交到财务去。他简单的一句话,干脆、利落、不拖泥...

  • 整洁干净

    昨天下班回家花了两个小时进行了大扫除,连加湿器也被我好好洗了一番。今天早上起来满是欣喜,终于没有五点半自然醒了,终...

  • 2021 年写 JavaScript 代码的 17 个优化技巧

    我们经常会写一些 JavaScript 代码,但是如何写出干净又易维护的代码呢?本文将讲解 17 个 JavaSc...

  • 女孩如何保持干净、整洁?

    现在各大平台都在宣传女性要精致,但是精致这个词太过笼统,没有任何标准可言 我一直都觉得,女孩子只要干净,整洁就已经...

  • 干净和整洁

    小的时候,关于着装,父母一直这样教育我,着装不一定要华丽,也不一定特别帅气,但一顶要“干净和整洁”。后来,这个习惯...

  • 干净整洁的家

    新家里面好久没有住人了,于是被尘封许久,趁着五一回家,来了个大扫除。 结果最可爱的事情就是我打扫的...

  • 整洁干净的家

    我经常在家里做卫生,我的家很整洁。 1:家里拖鞋鞋底每天擦洗一次,保持鞋底干净。 2:保持每天拖地一次,最好早上拖...

  • 家庭生活减压小妙招:收纳整理

    家是一个温暖的字。看到“家”这个字,我们往往会与温馨、干净、整洁联结在一起。那么如何打造一个干净整洁温暖的家呢?这...

网友评论

    本文标题:如何写出干净整洁的JavaScript

    本文链接:https://www.haomeiwen.com/subject/mfavbctx.html