最全面的前端开发指南

开发 后端 前端
可读性,正确性和可表达性优于性能。JavaScript基本上永远不会是你的性能瓶颈。图像压缩,网络接入和DOM重排来代替优化。如果从本文中你只能记住一个指导原则,那么毫无疑问就是这一条。

HTML

语义

HTML5为我们提供了很多旨在精确描述内容的语义元素。确保你可以从它丰富的词汇中获益。

  1. <!-- bad --> 
  2. <div id="main"
  3.   <div class="article"
  4.     <div class="header"
  5.       <h1>Blog post</h1> 
  6.       <p>Published: <span>21st Feb, 2015</span></p> 
  7.     </div> 
  8.     <p>…</p> 
  9.   </div> 
  10. </div> 
  11.  
  12. <!-- good --> 
  13. <main> 
  14.   <article> 
  15.     <header> 
  16.       <h1>Blog post</h1> 
  17.       <p>Published: <time datetime="2015-02-21">21st Feb, 2015</time></p> 
  18.     </header> 
  19.     <p>…</p> 
  20.   </article> 
  21. </main> 

你需要理解你正在使用的元素的语义。用一种错误的方式使用语义元素比保持中立更糟糕。

  1. <!-- bad --> 
  2. <h1> 
  3.   <figure> 
  4.     <img alt=Company src=logo.png> 
  5.   </figure> 
  6. </h1> 
  7.  
  8. <!-- good --> 
  9. <h1> 
  10.   <img alt=Company src=logo.png> 
  11. </h1> 

简洁

保持代码的简洁。忘记原来的XHTML习惯。

 

  1. <!-- bad --> 
  2. <!doctype html> 
  3. <html lang=en> 
  4.   <head> 
  5.     <meta http-equiv=Content-Type content="text/html; charset=utf-8" /> 
  6.     <title>Contact</title> 
  7.     <link rel=stylesheet href=style.css type=text/css /> 
  8.   </head> 
  9.   <body> 
  10.     <h1>Contact me</h1> 
  11.     <label> 
  12.       Email address: 
  13.       <input type=email placeholder=you@email.com required=required /> 
  14.     </label> 
  15.     <script src=main.js type=text/javascript></script> 
  16.   </body> 
  17. </html> 
  18.  
  19. <!-- good --> 
  20. <!doctype html> 
  21. <html lang=en> 
  22.   <meta charset=utf-8
  23.   <title>Contact</title> 
  24.   <link rel=stylesheet href=style.css> 
  25.  
  26.   <h1>Contact me</h1> 
  27.   <label> 
  28.     Email address: 
  29.     <input type=email placeholder=you@email.com required> 
  30.   </label> 
  31.   <script src=main.js></script> 
  32. </html> 

可访问性

可访问性不应该是以后再想的事情。提高网站不需要你成为一个WCAG专家,你完全可以通过修复一些小问题,从而造成一个巨大的变化,例如:

  • 学习正确使用alt 属性

  • 确保链接和按钮被同样地标记(不允许<div>)

  • 不专门依靠颜色来传递信息

  • 明确标注表单控件

  1. <!-- bad --> 
  2. <h1><img alt="Logo" src="logo.png"></h1> 
  3.  
  4. <!-- good --> 
  5. <h1><img alt="My Company, Inc." src="logo.png"></h1> 

语言

当定义语言和字符编码是可选择的时候,总是建议在文档级别同时声明,即使它们在你的HTTP标头已经详细说明。比任何其他字符编码更偏爱UTF-8。

  1. <!-- bad --> 
  2. <!doctype html> 
  3. <title>Hello, world.</title> 
  4. <!-- good --> 
  5. <!doctype html> 
  6. <html lang=en> 
  7.   <meta charset=utf-8
  8.   <title>Hello, world.</title> 
  9. </html> 

性能

除非有正当理由才能在内容前加载脚本,不要阻塞页面的渲染。如果你的样式表很重,开头就孤立那些绝对需要得样式,并在一个单独的样式表中推迟二次声明的加载。两个HTTP请求显然比一个慢,但是感知速度是最重要的因素。

  1. <!-- bad --> 
  2. <!doctype html> 
  3. <meta charset=utf-8
  4. <script src=analytics.js></script> 
  5. <title>Hello, world.</title> 
  6. <p>...</p> 
  7.  
  8. <!-- good --> 
  9. <!doctype html> 
  10. <meta charset=utf-8
  11. <title>Hello, world.</title> 
  12. <p>...</p> 
  13. <script src=analytics.js></script> 

CSS

分号

虽然分号在技术上是CSS一个分隔符,但应该始终把它作为一个终止符。

  1. /* bad */ 
  2. div { 
  3.   color: red 
  4.  
  5. /* good */ 
  6. div { 
  7.   color: red; 

盒子模型

盒子模型对于整个文档而言最好是相同的。全局性的* { box-sizing: border-box; }就非常不错,但是不要改变默认盒子模型的特定元素,如果可以避免的话。

  1. /* bad */ 
  2. div { 
  3.   width: 100%; 
  4.   padding: 10px; 
  5.   box-sizing: border-box; 
  6.  
  7. /* good */ 
  8. div { 
  9.   padding: 10px; 

不要更改元素的默认行为,如果可以避免的话。元素尽可能地保持在自然的文档流中。例如,删除图像下方的空格而不改变其默认显示:

  1. /* bad */ 
  2. img { 
  3.   display: block; 
  4.  
  5. /* good */ 
  6. img { 
  7.   vertical-align: middle; 

同样,如果可以避免的话,不要关闭元素流。

  1. /* bad */ 
  2. div { 
  3.   width: 100px; 
  4.   position: absolute; 
  5.   right: 0
  6.  
  7. /* good */ 
  8. div { 
  9.   width: 100px; 
  10.   margin-left: auto; 

定位

在CSS中有许多定位元素的方法,但应该尽量限制以下属性/值。按优先顺序排列:

  1. display: block; 
  2. display: flex; 
  3. position: relative; 
  4. position: sticky; 
  5. position: absolute; 
  6. position: fixed; 

选择器

最小化紧密耦合到DOM的选择器。当选择器有多于3个结构伪类,后代或兄弟选择器的时候,考虑添加一个类到你想匹配的元素。

  1. /* bad */ 
  2. div:first-of-type :last-child > p ~ * 
  3.  
  4. /* good */ 
  5. div:first-of-type .info 

当你不需要的时候避免过载选择器。

  1. /* bad */ 
  2. img[src$=svg], ul > li:first-child { 
  3.   opacity: 0
  4.  
  5. /* good */ 
  6. [src$=svg], ul > :first-child { 
  7.   opacity: 0

特异性

不要让值和选择器难以覆盖。尽量少用id,并避免!important。

  1. /* bad */ 
  2. .bar { 
  3.   color: green !important; 
  4. .foo { 
  5.   color: red; 
  6.  
  7. /* good */ 
  8. .foo.bar { 
  9.   color: green; 
  10. .foo { 
  11.   color: red; 

覆盖

覆盖样式使得选择器和调试变得困难。如果可能的话,避免覆盖样式。

  1. /* bad */ 
  2. li { 
  3.   visibility: hidden; 
  4. li:first-child { 
  5.   visibility: visible; 
  6.  
  7. /* good */ 
  8. li + li { 
  9.   visibility: hidden; 

继承

不要重复可以继承的样式声明。

  1. /* bad */ 
  2. div h1, div p { 
  3.   text-shadow: 0 1px 0 #fff; 
  4.  
  5. /* good */ 
  6. div { 
  7.   text-shadow: 0 1px 0 #fff; 

简洁

保持代码的简洁。使用简写属性,没有必要的话,要避免使用多个属性。

  1. /* bad */ 
  2. div { 
  3.   transition: all 1s; 
  4.   top: 50%; 
  5.   margin-top: -10px; 
  6.   padding-top: 5px; 
  7.   padding-right: 10px; 
  8.   padding-bottom: 20px; 
  9.   padding-left: 10px; 
  10.  
  11. /* good */ 
  12. div { 
  13.   transition: 1s; 
  14.   top: calc(50% - 10px); 
  15.   padding: 5px 10px 20px; 

语言

英语表达优于数学公式。

  1. /* bad */ 
  2. :nth-child(2n + 1) { 
  3.   transform: rotate(360deg); 
  4.  
  5. /* good */ 
  6. :nth-child(odd) { 
  7.   transform: rotate(1turn); 

浏览器引擎前缀

果断地删除过时的浏览器引擎前缀。如果需要使用的话,可以在标准属性前插入它们。

  1. /* bad */ 
  2. div { 
  3.   transform: scale(2); 
  4.   -webkit-transform: scale(2); 
  5.   -moz-transform: scale(2); 
  6.   -ms-transform: scale(2); 
  7.   transition: 1s; 
  8.   -webkit-transition: 1s; 
  9.   -moz-transition: 1s; 
  10.   -ms-transition: 1s; 
  11.  
  12. /* good */ 
  13. div { 
  14.   -webkit-transform: scale(2); 
  15.   transform: scale(2); 
  16.   transition: 1s; 

动画

视图转换优于动画。除了opacity 和transform,避免动画其他属性。

  1. /* bad */ 
  2. div:hover { 
  3.   animation: move 1s forwards; 
  4. @keyframes move { 
  5.   100% { 
  6.     margin-left: 100px; 
  7.   } 
  8.  
  9. /* good */ 
  10. div:hover { 
  11.   transition: 1s; 
  12.   transform: translateX(100px); 

单位

可以的话,使用无单位的值。如果使用相对单位,那就用rem 。秒优于毫秒。

  1. /* bad */ 
  2. div { 
  3.   margin: 0px; 
  4.   font-size: .9em; 
  5.   line-height: 22px; 
  6.   transition: 500ms; 
  7.  
  8. /* good */ 
  9. div { 
  10.   margin: 0
  11.   font-size: .9rem; 
  12.   line-height: 1.5
  13.   transition: .5s; 

颜色

如果你需要透明度,使用rgba。另外,始终使用十六进制格式。

  1. /* bad */ 
  2. div { 
  3.   color: hsl(10354%, 43%); 
  4.  
  5. /* good */ 
  6. div { 
  7.   color: #5a3; 

绘画

当资源很容易用CSS复制的时候,避免HTTP请求。

  1. /* bad */ 
  2. div::before { 
  3.   content: url(white-circle.svg); 
  4.  
  5. /* good */ 
  6. div::before { 
  7.   content: ""
  8.   display: block; 
  9.   width: 20px; 
  10.   height: 20px; 
  11.   border-radius: 50%; 
  12.   background: #fff; 

Hacks

不要使用Hacks。

  1. /* bad */ 
  2. div { 
  3.   // position: relative; 
  4.   transform: translateZ(0); 
  5.  
  6. /* good */ 
  7. div { 
  8.   /* position: relative; */ 
  9.   will-change: transform; 

JavaScript

性能

可读性,正确性和可表达性优于性能。JavaScript基本上永远不会是你的性能瓶颈。图像压缩,网络接入和DOM重排来代替优化。如果从本文中你只能记住一个指导原则,那么毫无疑问就是这一条。

  1. // bad (albeit way faster) 
  2. const arr = [1234]; 
  3. const len = arr.length; 
  4. var i = -1
  5. var result = []; 
  6. while (++i < len) { 
  7.   var n = arr[i]; 
  8.   if (n % 2 > 0continue
  9.   result.push(n * n); 
  10.  
  11. // good 
  12. const arr = [1234]; 
  13. const isEven = n => n % 2 == 0
  14. const square = n => n * n; 
  15.  
  16. const result = arr.filter(isEven).map(square); 

无状态

尽量保持函数纯洁。理论上,所有函数都不会产生副作用,不会使用外部数据,并且会返回新对象,而不是改变现有的对象。

  1. // bad 
  2. const merge = (target, ...sources) => Object.assign(target, ...sources); 
  3. merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" } 
  4.  
  5. // good 
  6. const merge = (...sources) => Object.assign({}, ...sources); 
  7. merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" } 

本地化

尽可能地依赖本地方法。

  1. // bad 
  2. const toArray = obj => [].slice.call(obj); 
  3.  
  4. // good 
  5. const toArray = (() => 
  6.   Array.from ? Array.from : obj => [].slice.call(obj) 
  7. )(); 

强制性

如果强制有意义,那么就使用隐式强制。否则就应该避免强制。

  1. // bad 
  2. if (x === undefined || x === null) { ... } 
  3.  
  4. // good 
  5. if (x == undefined) { ... } 

循环

不要使用循环,因为它们会强迫你使用可变对象。依靠array.prototype 方法。

  1. // bad 
  2. const sum = arr => { 
  3.   var sum = 0
  4.   var i = -1
  5.   for (;arr[++i];) { 
  6.     sum += arr[i]; 
  7.   } 
  8.   return sum; 
  9. }; 
  10.  
  11. sum([123]); // => 6 
  12.  
  13. // good 
  14. const sum = arr => 
  15.   arr.reduce((x, y) => x + y); 
  16.  
  17. sum([123]); // => 6 

如果不能避免,或使用array.prototype 方法滥用了,那就使用递归。

  1. // bad 
  2. const createDivs = howMany => { 
  3.   while (howMany--) { 
  4.     document.body.insertAdjacentHTML("beforeend""<div></div>"); 
  5.   } 
  6. }; 
  7. createDivs(5); 
  8.  
  9. // bad 
  10. const createDivs = howMany => 
  11.   [...Array(howMany)].forEach(() => 
  12.     document.body.insertAdjacentHTML("beforeend""<div></div>"
  13.   ); 
  14. createDivs(5); 
  15.  
  16. // good 
  17. const createDivs = howMany => { 
  18.   if (!howMany) return
  19.   document.body.insertAdjacentHTML("beforeend""<div></div>"); 
  20.   return createDivs(howMany - 1); 
  21. }; 
  22. createDivs(5); 

这里有一个通用的循环功能,可以让递归更容易使用。

参数

忘记arguments 对象。余下的参数往往是一个更好的选择,这是因为:

你可以从它的命名中更好地了解函数需要什么样的参数

真实数组,更易于使用。

  1. // bad 
  2. const sortNumbers = () => 
  3.   Array.prototype.slice.call(arguments).sort(); 
  4.  
  5. // good 
  6. const sortNumbers = (...numbers) => numbers.sort(); 

应用

忘掉apply()。使用操作符。

  1. const greet = (first, last) => `Hi ${first} ${last}`; 
  2. const person = ["John""Doe"]; 
  3.  
  4. // bad 
  5. greet.apply(null, person); 
  6.  
  7. // good 
  8. greet(...person); 

绑定

当有更惯用的做法时,就不要用bind() 。

 

  1. // bad 
  2. ["foo""bar"].forEach(func.bind(this)); 
  3.  
  4. // good 
  5. ["foo""bar"].forEach(func, this); 
  6.  
  7. // bad 
  8. const person = { 
  9.   first: "John"
  10.   last: "Doe"
  11.   greet() { 
  12.     const full = function() { 
  13.       return `${this.first} ${this.last}`; 
  14.     }.bind(this); 
  15.     return `Hello ${full()}`; 
  16.   } 
  17.  
  18. // good 
  19. const person = { 
  20.   first: "John"
  21.   last: "Doe"
  22.   greet() { 
  23.     const full = () => `${this.first} ${this.last}`; 
  24.     return `Hello ${full()}`; 
  25.   } 

函数嵌套

没有必要的话,就不要嵌套函数。

  1. // bad 
  2. [123].map(num => String(num)); 
  3.  
  4. // good 
  5. [123].map(String); 

合成函数

避免调用多重嵌套函数。使用合成函数来替代。

  1. const plus1 = a => a + 1
  2. const mult2 = a => a * 2
  3.  
  4. // bad 
  5. mult2(plus1(5)); // => 12 
  6.  
  7. // good 
  8. const pipeline = (...funcs) => val => funcs.reduce((a, b) => b(a), val); 
  9. const addThenMult = pipeline(plus1, mult2); 
  10. addThenMult(5); // => 12 

缓存

缓存功能测试,大数据结构和任何奢侈的操作。

  1. // bad 
  2. const contains = (arr, value) => 
  3.   Array.prototype.includes 
  4.     ? arr.includes(value) 
  5.     : arr.some(el => el === value); 
  6. contains(["foo""bar"], "baz"); // => false 
  7.  
  8. // good 
  9. const contains = (() => 
  10.   Array.prototype.includes 
  11.     ? (arr, value) => arr.includes(value) 
  12.     : (arr, value) => arr.some(el => el === value) 
  13. )(); 
  14. contains(["foo""bar"], "baz"); // => false 

变量

const 优于let ,let 优于var。

  1. // bad 
  2. var me = new Map(); 
  3. me.set("name""Ben").set("country""Belgium"); 
  4.  
  5. // good 
  6. const me = new Map(); 
  7. me.set("name""Ben").set("country""Belgium"); 

条件

IIFE 和return 语句优于if, else if,else和switch语句。

  1. // bad 
  2. var grade; 
  3. if (result < 50
  4.   grade = "bad"
  5. else if (result < 90
  6.   grade = "good"
  7. else 
  8.   grade = "excellent"
  9.  
  10. // good 
  11. const grade = (() => { 
  12.   if (result < 50
  13.     return "bad"
  14.   if (result < 90
  15.     return "good"
  16.   return "excellent"
  17. })(); 

对象迭代

如果可以的话,避免for…in。

  1. const shared = { foo: "foo" }; 
  2. const obj = Object.create(shared, { 
  3.   bar: { 
  4.     value: "bar"
  5.     enumerable: true 
  6.   } 
  7. }); 
  8.  
  9. // bad 
  10. for (var prop in obj) { 
  11.   if (obj.hasOwnProperty(prop)) 
  12.     console.log(prop); 
  13.  
  14. // good 
  15. Object.keys(obj).forEach(prop => console.log(prop)); 

map对象

在对象有合法用例的情况下,map通常是一个更好,更强大的选择。

  1. // bad 
  2. const me = { 
  3.   name: "Ben"
  4.   age: 30 
  5. }; 
  6. var meSize = Object.keys(me).length; 
  7. meSize; // => 2 
  8. me.country = "Belgium"
  9. meSize++; 
  10. meSize; // => 3 
  11.  
  12. // good 
  13. const me = new Map(); 
  14. me.set("name""Ben"); 
  15. me.set("age"30); 
  16. me.size; // => 2 
  17. me.set("country""Belgium"); 
  18. me.size; // => 3 

Curry

Curry虽然功能强大,但对于许多开发人员来说是一个外来的范式。不要滥用,因为其视情况而定的用例相当不寻常。

  1. // bad 
  2. const sum = a => b => a + b; 
  3. sum(5)(3); // => 8 
  4.  
  5. // good 
  6. const sum = (a, b) => a + b; 
  7. sum(53); // => 8 

可读性

不要用看似聪明的伎俩混淆代码的意图。

 

  1. // bad 
  2. foo || doSomething(); 
  3.  
  4. // good 
  5. if (!foo) doSomething(); 
  6.  
  7. // bad 
  8. void function() { /* IIFE */ }(); 
  9.  
  10. // good 
  11. (function() { /* IIFE */ }()); 
  12.  
  13. // bad 
  14. const n = ~~3.14
  15.  
  16. // good 
  17. const n = Math.floor(3.14); 

代码重用

不要害怕创建小型的,高度可组合的,可重复使用的函数。

 

  1. // bad 
  2. arr[arr.length - 1]; 
  3.  
  4. // good 
  5. const first = arr => arr[0]; 
  6. const last = arr => first(arr.slice(-1)); 
  7. last(arr); 
  8.  
  9. // bad 
  10. const product = (a, b) => a * b; 
  11. const triple = n => n * 3
  12.  
  13. // good 
  14. const product = (a, b) => a * b; 
  15. const triple = product.bind(null3); 

依赖性

最小化依赖性。第三方是你不知道的代码。不要只是因为几个可轻易复制的方法而加载整个库:

  1. // bad 
  2. var _ = require("underscore"); 
  3. _.compact(["foo"0])); 
  4. _.unique(["foo""foo"]); 
  5. _.union(["foo"], ["bar"], ["foo"]); 
  6.  
  7. // good 
  8. const compact = arr => arr.filter(el => el); 
  9. const unique = arr => [...Set(arr)]; 
  10. const union = (...arr) => unique([].concat(...arr)); 
  11.  
  12. compact(["foo"0]); 
  13. unique(["foo""foo"]); 
  14. union(["foo"], ["bar"], ["foo"]); 

译文链接:http://www.codeceo.com/article/full-frontend-guidelines.html
英文原文:Frontend Guidelines

 

 

 

责任编辑:王雪燕 来源: 码农网
相关推荐

2023-05-15 18:44:07

前端开发

2022-08-11 10:43:23

前端开发实践

2022-08-17 11:33:35

前端配置

2017-03-29 09:08:25

Spring笔记

2013-05-27 14:06:14

Android开发移动开发Intent机制

2011-07-25 16:21:22

Sencha touc

2022-08-02 08:01:09

开发插件Chrome前端技术

2015-09-18 16:55:45

云计算

2016-01-28 14:41:06

CC++编码

2015-11-17 09:30:23

程序员招聘建议

2011-06-09 18:24:36

QT Wince

2012-03-26 09:27:40

谷歌安卓开发谷歌安卓

2009-06-24 16:30:21

JSF组件模型

2017-02-05 09:13:58

PHP Cake框架构建

2015-11-12 16:14:52

Python开发实践

2010-06-13 09:27:56

Widget开发

2019-10-31 08:00:00

机器学习人工智能AI

2011-04-18 11:00:34

使用音频BlackBerry

2012-05-18 10:08:56

TitaniumAndroid

2011-12-29 10:48:49

移动Web
点赞
收藏

51CTO技术栈公众号