HTML5技术

GitHub 上一份很受欢迎的前端代码优化指南-强烈推荐收藏 - yonghu86(3)

字号+ 作者:H5之家 来源:H5之家 2015-12-04 15:46 我要评论( )

const plus1 = a = a + 1;const mult2 = a = a * 2;// badmult2(plus1(5)); // = 12// goodconst pipeline = (...funcs) = val = funcs.reduce((a, b) = b(a), val);const addThenMult = pipeline(plus1, mult2);ad

const plus1 = a => a + 1; const mult2 = a => a * 2; // bad mult2(plus1(5)); // => 12 // good const pipeline = (...funcs) => val => funcs.reduce((a, b) => b(a), val); const addThenMult = pipeline(plus1, mult2); addThenMult(5); // => 12

缓存

缓存性能测试,大数据结构和任何高代价的操作。

// bad const contains = (arr, value) => Array.prototype.includes ? arr.includes(value) : arr.some(el => el === value); contains(["foo", "bar"], "baz"); // => false // good const contains = (() => Array.prototype.includes ? (arr, value) => arr.includes(value) : (arr, value) => arr.some(el => el === value) )(); contains(["foo", "bar"], "baz"); // => false

变量

const 优于 let, let 优于 var。

// bad var obj = {}; obj["foo" + "bar"] = "baz"; // good const obj = { ["foo" + "bar"]: "baz" };

条件判断

用多个if,优于 if 、else if、else 和switch。

// bad var grade; if (result < 50) grade = "bad"; else if (result < 90) grade = "good"; else grade = "excellent"; // good const grade = (() => { if (result < 50) return "bad"; if (result < 90) return "good"; return "excellent"; })();

对象的操作

避免使用for...in。

const shared = { foo: "foo" }; const obj = Object.create(shared, { bar: { value: "bar", enumerable: true } }); // bad for (var prop in obj) { if (obj.hasOwnProperty(prop)) console.log(prop); } // good Object.keys(obj).forEach(prop => console.log(prop));

使用map

合理使用的情况下,map更强大:

// bad const me = { name: "Ben", age: 30 }; var meSize = Object.keys(me).length; meSize; // => 2 me.country = "Belgium"; meSize++; meSize; // => 3 // good const me = Map(); me.set("name", "Ben"); me.set("age", 30); me.size; // => 2 me.set("country", "Belgium"); me.size; // => 3

Curry

在别的语言里有Curry的一席之地,但在JS里避免使用。不然会是代码阅读困难。

// bad const sum = a => b => a + b; sum(5)(3); // => 8 // good const sum = (a, b) => a + b; sum(5, 3); // => 8

可读性

不要使用自以为是的技巧:

// bad foo || doSomething(); // good if (!foo) doSomething(); // bad void function() { /* IIFE */ }(); // good (function() { /* IIFE */ }()); // bad const n = ~~3.14; // good const n = Math.floor(3.14);

代码重用

对写些小型、组件化、可重用的方法。

// bad arr[arr.length - 1]; // good const first = arr => arr[0]; const last = arr => first(arr.slice(-1)); last(arr); // bad const product = (a, b) => a * b; const triple = n => n * 3; // good const product = (a, b) => a * b; const triple = product.bind(null, 3);

依赖

减少第三方库的使用。当你无法完成某项工作时可以使用,但不要为了一些能自己实现的小功能就加载一个很大的库。

// bad var _ = require("underscore"); _.compact(["foo", 0])); _.unique(["foo", "foo"]); _.union(["foo"], ["bar"], ["foo"]); // good const compact = arr => arr.filter(el => el); const unique = arr => [...Set(arr)]; const union = (...arr) => unique([].concat(...arr)); compact(["foo", 0]); unique(["foo", "foo"]); union(["foo"], ["bar"], ["foo"]);

  

英文原文 Frontend Guidelines

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • GitHub开源:升讯威ADO.NET增强组件 sheng.ADO.NET.Plus V1.3 - sheng.chao

    GitHub开源:升讯威ADO.NET增强组件 sheng.ADO.NET.Plus V1.3 - shen

    2017-03-22 13:02

  • 一份关于组建.NET Core开源团队的倡议书 - 彭泽0902

    一份关于组建.NET Core开源团队的倡议书 - 彭泽0902

    2017-03-13 17:02

  • 一个想法(续三):一份IT技术联盟创业计划书,开启众筹创业征程 - 路过秋天

    一个想法(续三):一份IT技术联盟创业计划书,开启众筹创业征程 -

    2017-02-10 11:00

  • 【福利大放送】不止是Android,Github超高影响力开源大放送,学习开发必备教科书 - 南尘

    【福利大放送】不止是Android,Github超高影响力开源大放送,学习开

    2017-02-09 12:04

网友点评