前端代码书写规范

来源:互联网 发布:全球经济数据库 市盈率 编辑:程序博客网 时间:2024/06/05 14:49

看了以下这个网址,不错哦。

http://www.css88.com/archives/5361

这是一份旨在增强团队的开发协作,提高代码质量和打造开发基石的编码风格规范,其中包含了 HTML, JavaScript 和 CSS/SCSS 这几个部分。我们知道,当一个团队开始指定并实行编码规范的话,错误就会变得更加显而易见。如果一段特定的代码不符合规范的话,它有可能只是代码风格错误,而也有可能会是 bug。早期指定规范就使得代码审核得以更好的开展,并且可以更精确的地定位到错误。只要开发者们能够保证源代码源文件都严格遵循规范,那接下去所使用的混淆、压缩和编译工具则可投其所好不尽相同。


一般规范

以下章节列举了一些可应用在 HTML, JavaScript 和 CSS/SCSS 上的通用规则。


文件/资源命名

在 web 项目中,所有的文件名应该都遵循同一命名约定。以可读性而言,减号(-)是用来分隔文件名的不二之选。同时它也是常见的 URL 分隔符(i.e. //example.com/blog/my-blog-entry or //s.example.com/images/big-black-background.jpg),所以理所当然的,减号应该也是用来分隔资源名称的好选择。

请确保文件命名总是以字母开头而不是数字。而以特殊字符开头命名的文件,一般都有特殊的含义与用处(比如 compass[1] 中的下划线就是用来标记跳过直接编译的文件用的)。

资源的字母名称必须全为小写,这是因为在某些对大小写字母敏感的操作系统中,当文件通过工具压缩混淆后,或者人为修改过后,大小写不同而导致引用文件不同的错误,很难被发现。

还有一些情况下,需要对文件增加前后缀或特定的扩展名(比如 .min.js, .min.css),抑或一串前缀(比如 3fa89b.main.min.css)。这种情况下,建议使用点分隔符来区分这些在文件名中带有清晰意义的元数据。

不推荐

html 代码:
  1. MyScript.js
  2. myCamelCaseName.css
  3. i_love_underscores.html
  4. 1001-scripts.js
  5. my-file-min.css

推荐

html 代码:
  1. my-script.js
  2. my-camel-case-name.css
  3. i-love-underscores.html
  4. thousand-and-one-scripts.js
  5. my-file.min.css

协议

不要指定引入资源所带的具体协议。

当引入图片或其他媒体文件,还有样式和脚本时,URLs 所指向的具体路径,不要指定协议部分(http:https:),除非这两者协议都不可用。

不指定协议使得 URL 从绝对的获取路径转变为相对的,在请求资源协议无法确定时非常好用,而且还能为文件大小节省几个字节。

不推荐

html 代码:
  1. <script src="http://cdn.com/foundation.min.js"></script>

推荐

html 代码:
  1. <script src="//cdn.com/foundation.min.js"></script>

不推荐

css 代码:
  1. .example {
  2. background: url(http://static.example.com/images/bg.jpg);
  3. }

推荐

css 代码:
  1. .example {
  2. background: url(//static.example.com/images/bg.jpg);
  3. }

文本缩进

一次缩进两个空格。

html 代码:
  1. <ul>
  2. <li>Fantastic</li>
  3. <li>Great</li>
  4. <li>
  5. <a href="#">Test</a>
  6. </li>
  7. </ul>
css 代码:
  1. @media screen and (min-width: 1100px) {
  2. body {
  3. font-size: 100%;
  4. }
  5. }
js 代码:
  1. (function(){
  2. var x = 10;
  3.  
  4. function y(a, b) {
  5. return {
  6. result: (a + b) * x
  7. }
  8.  
  9. }
  10. }());

注释

注释是你自己与你的小伙伴们了解代码写法和目的的唯一途径。特别是在写一些看似琐碎的无关紧要的代码时,由于记忆点不深刻,注释就变得尤为重要了。

编写自解释代码只是一个传说,没有任何代码是可以完全自解释的。而代码注释,则是永远也不嫌多。

当你写注释时一定要注意:不要写你的代码都干了些什么,而要写你的代码为什么要这么写,背后的考量是什么。当然也可以加入所思考问题或是解决方案的链接地址。

不推荐

js 代码:
  1. var offset = 0;
  2.  
  3. if(includeLabels) {
  4. // Add offset of 20
  5. offset = 20;
  6. }

推荐

js 代码:
  1. var offset = 0;
  2.  
  3. if(includeLabels) {
  4. // If the labels are included we need to have a minimum offset of 20 pixels
  5. // We need to set it explicitly because of the following bug: http://somebrowservendor.com/issue-tracker/ISSUE-1
  6. offset = 20;
  7. }

一些注释工具可以帮助你写出更好的注释。JSDoc 或 YUIDoc 就是用来写 JavaScript 注释用的。你甚至可以使用工具来为这些注释生成文档,这也是激励开发者们写注释的一个好方法,因为一旦有了这样方便的生成文档的工具,他们通常会开始花更多时间在注释细节上。


css 代码:
  1.  

代码检查

对于比较宽松自由的编程语言来说,严格遵循编码规范和格式化风格指南就显得极为重要。遵循规范固然很好,但是有自动化流程来确保其执行情况,岂不更佳。Trust is good, control is better.

对于 JavaScript,建议使用 JSLint 或 JSHint。



HTML 规范


文档类型

推荐使用 HTML5 的文档类型申明: <!DOCTYPE html>.

(建议使用 text/html 格式的 HTML。避免使用 XHTML。XHTML 以及它的属性,比如 application/xhtml+xml 在浏览器中的应用支持与优化空间都十分有限)。

HTML 中最好不要将无内容元素[1] 的标签闭合,例如:使用 <br> 而非 <br />.


HTML 验证

一般情况下,建议使用能通过标准规范验证的 HTML 代码,除非在性能优化和控制文件大小上不得不做出让步。

使用诸如 W3C HTML validator 这样的工具来进行检测。

规范化的 HTML 是显现技术要求与局限的显著质量基线,它促进了 HTML 被更好地运用。

不推荐

html 代码:
  1. <title>Test</title>
  2. <article>This is only a test.

推荐

html 代码:
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <title>Test</title>
  4. <article>This is only a test.</article>

省略可选标签

HTML5 规范中规定了 HTML 标签是可以省略的。但从可读性来说,在开发的源文件中最好不要这样做,因为省略标签可能会导致一些问题。

省略一些可选的标签确实使得页面大小减少,这很有用,尤其是对于一些大型网站来说。为了达到这一目的,我们可以在开发后期对页面进行压缩处理,在这个环节中这些可选的标签完全就可以省略掉了。


脚本加载

出于性能考虑,脚本异步加载很关键。一段脚本放置在 <head> 内,比如 <script src="main.js"></script>,其加载会一直阻塞 DOM 解析,直至它完全地加载和执行完毕。这会造成页面显示的延迟。特别是一些重量级的脚本,对用户体验来说那真是一个巨大的影响。

异步加载脚本可缓解这种性能影响。如果只需兼容 IE10+,可将 HTML5 的 async 属性加至脚本中,它可防止阻塞 DOM 的解析,甚至你可以将脚本引用写在 <head> 里也没有影响。

如需兼容老旧的浏览器,实践表明可使用用来动态注入脚本的脚本加载器。你可以考虑 yepnope 或labjs。注入脚本的一个问题是:一直要等到 CSS 对象文档已就绪,它们才开始加载(短暂地在 CSS 加载完毕之后),这就对需要及时触发的 JS 造成了一定的延迟,这多多少少也影响了用户体验吧。

终上所述,兼容老旧浏览器(IE9-)时,应该遵循以下最佳实践。

脚本引用写在 body 结束标签之前,并带上 async 属性。这虽然在老旧浏览器中不会异步加载脚本,但它只阻塞了 body 结束标签之前的 DOM 解析,这就大大降低了其阻塞影响。而在现代浏览器中,脚本将在 DOM 解析器发现 body 尾部的 script 标签才进行加载,此时加载属于异步加载,不会阻塞 CSSOM(但其执行仍发生在 CSSOM 之后)。

所有浏览器中,推荐

js 代码:
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="main.css">
  4. </head>
  5. <body>
  6. <!-- body goes here -->
  7.  
  8. <script src="main.js" async></script>
  9. </body>
  10. </html>

只在现代浏览器中,推荐


html 代码:
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="main.css">
  4. <script src="main.js" async></script>
  5. </head>
  6. <body>
  7. <!-- body goes here -->
  8. </body>
  9. </html>

语义化

根据元素(有时被错误地称作“标签”)其被创造出来时的初始意义来使用它。打个比方,用 heading 元素来定义头部标题,p 元素来定义文字段落,用 a 元素来定义链接锚点,等等。

有根据有目的地使用 HTML 元素,对于可访问性、代码重用、代码效率来说意义重大。

以下示例列出了一些的语义化 HTML 主要情况:

不推荐

html 代码:
  1. <b>My page title</b>
  2. <div class="top-navigation">
  3. <div class="nav-item"><a href="#home">Home</a></div>
  4. <div class="nav-item"><a href="#news">News</a></div>
  5. <div class="nav-item"><a href="#about">About</a></div>
  6. </div>
  7.  
  8. <div class="news-page">
  9. <div class="page-section news">
  10. <div class="title">All news articles</div>
  11. <div class="news-article">
  12. <h2>Bad article</h2>
  13. <div class="intro">Introduction sub-title</div>
  14. <div class="content">This is a very bad example for HTML semantics</div>
  15. <div class="article-side-notes">I think I'm more on the side and should not receive the main credits</div>
  16. <div class="article-foot-notes">
  17. This article was created by David <div class="time">2014-01-01 00:00</div>
  18. </div>
  19. </div>
  20.  
  21. <div class="section-footer">
  22. Related sections: Events, Public holidays
  23. </div>
  24. </div>
  25. </div>
  26.  
  27. <div class="page-footer">
  28. Copyright 2014
  29. </div>

推荐

html 代码:
  1. <!-- The page header should go into a header element -->
  2. <header>
  3. <!-- As this title belongs to the page structure it's a heading and h1 should be used -->
  4. <h1>My page title</h1>
  5. </header>
  6.  
  7. <!-- All navigation should go into a nav element -->
  8. <nav class="top-navigation">
  9. <!-- A listing of elements should always go to UL (OL for ordered listings) -->
  10. <ul>
  11. <li class="nav-item"><a href="#home">Home</a></li>
  12. <li class="nav-item"><a href="#news">News</a></li>
  13. <li class="nav-item"><a href="#about">About</a></li>
  14. </ul>
  15. </nav>
  16.  
  17. <!-- The main part of the page should go into a main element (also use role="main" for accessibility) -->
  18. <main class="news-page" role="main">
  19. <!-- A section of a page should go into a section element. Divide a page into sections with semantic elements. -->
  20. <section class="page-section news">
  21. <!-- A section header should go into a section element -->
  22. <header>
  23. <!-- As a page section belongs to the page structure heading elements should be used (in this case h2) -->
  24. <h2 class="title">All news articles</h2>
  25. </header>
  26.  
  27. <!-- If a section / module can be seen as an article (news article, blog entry, products teaser, any other
  28. re-usable module / section that can occur multiple times on a page) a article element should be used -->
  29. <article class="news-article">
  30. <!-- An article can contain a header that contains the summary / introduction information of the article -->
  31. <header>
  32. <!-- As a article title does not belong to the overall page structure there should not be any heading tag! -->
  33. <div class="article-title">Good article</div>
  34. <!-- Small can optionally be used to reduce importance -->
  35. <small class="intro">Introduction sub-title</small>
  36. </header>
  37.  
  38. <!-- For the main content in a section or article there is no semantic element -->
  39. <div class="content">
  40. <p>This is a good example for HTML semantics</p>
  41. </div>
  42. <!-- For content that is represented as side note or less important information in a given context use aside -->
  43. <aside class="article-side-notes">
  44. <p>I think I'm more on the side and should not receive the main credits</p>
  45. </aside>
  46. <!-- Articles can also contain footers. If you have footnotes for an article place them into a footer element -->
  47. <footer class="article-foot-notes">
  48. <!-- The time element can be used to annotate a timestamp. Use the datetime attribute to specify ISO time
  49. while the actual text in the time element can also be more human readable / relative -->
  50. <p>This article was created by David <time datetime="2014-01-01 00:00" class="time">1 month ago</time></p>
  51. </footer>
  52. </article>
  53.  
  54. <!-- In a section, footnotes or similar information can also go into a footer element -->
  55. <footer class="section-footer">
  56. <p>Related sections: Events, Public holidays</p>
  57. </footer>
  58. </section>
  59. </main>
  60.  
  61. <!-- Your page footer should go into a global footer element -->
  62. <footer class="page-footer">
  63. Copyright 2014
  64. </footer>

多媒体回溯

对页面上的媒体而言,像图片、视频、canvas 动画等,要确保其有可替代的接入接口。图片文件我们可采用有意义的备选文本(alt),视频和音频文件我们可以为其加上说明文字或字幕。

提供可替代内容对可用性来说十分重要。试想,一位盲人用户如何能知晓一张图片是什么,要是没有 @alt 的话。

(图片的 alt 属性是可不填写内容的,纯装饰性的图片就可用这么做:alt="")。

不推荐

html 代码:
  1. <img src="luke-skywalker.jpg">

推荐

html 代码:
  1. <img src="luke-skywalker.jpg" alt="Luke skywalker riding an alien horse">

尽量用 alt 标签去描述图片,设想你需要对于那些只能通过语音或者看不见图片的用户表达图片到底是什么。

不推荐

html 代码:
  1. <img src="huge-spaceship-approaching-earth.jpg" alt="Header image">

推荐

html 代码:
  1. <img src="huge-spaceship-approaching-earth.jpg" alt="A huge spaceship that is approaching the earth">

关注点分离

理解 web 中如何和为何区分不同的关注点,这很重要。这里的关注点主要指的是:信息(HTML 结构)、外观(CSS)和行为(JavaScript)。为了使它们成为可维护的干净整洁的代码,我们要尽可能的将它们分离开来。

严格地保证结构、表现、行为三者分离,并尽量使三者之间没有太多的交互和联系。

就是说,尽量在文档和模板中只包含结构性的 HTML;而将所有表现代码,移入样式表中;将所有动作行为,移入脚本之中。

在此之外,为使得它们之间的联系尽可能的小,在文档和模板中也尽量少地引入样式和脚本文件。

清晰的分层意味着:

  • 不使用超过一到两张样式表(i.e. main.css, vendor.css)
  • 不使用超过一到两个脚本(学会用合并脚本)
  • 不使用行内样式(<style>.no-good {}</style>
  • 不在元素上使用 style 属性(<hr style="border-top: 5px solid black">
  • 不使用行内脚本(<script>alert('no good')</script>
  • 不使用表象元素(i.e. <b><u><center><font><b>
  • 不使用表象 class 名(i.e. red, left, center)

不推荐

html 代码:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="base.css">
  5. <link rel="stylesheet" href="grid.css">
  6. <link rel="stylesheet" href="type.css">
  7. <link rel="stylesheet" href="modules/teaser.css">
  8. </head>
  9. <body>
  10. <h1 style="font-size: 3rem"></h1>
  11. <b>I'm a subtitle and I'm bold!</b>
  12. <center>Dare you center me!</center>
  13. <script>
  14. alert('Just dont...');
  15. </script>
  16. <div class="red">I'm important!</div>
  17. </body>
  18. </html>

推荐

html 代码:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Concatinate your style sheets into a single one -->
  5. <link rel="stylesheet" href="main.css">
  6. </head>
  7. <body>
  8. <!-- Don't use style attributes but assign sensible classes and apply styles in the stylesheet -->
  9. <h1 class="title"></h1>
  10. <!-- Don't use presentational elements and assign sensible classes -->
  11. <div class="sub-title">I'm a subtitle and I'm bold!</div>
  12. <!-- Maybe your comments get centered in your presentation but that decision is up to the stylesheet -->
  13. <span class="comment">Dare you center me!</span>
  14. <!-- You wanted to make it red because it's important so then also name the class important and decide in the stylesheet
  15. what you want to do with it -->
  16. <div class="important">I'm important!</div>
  17.  
  18. <!-- Put all your scripts into files and concatinate them into a single one -->
  19. <script async src="main.js"></script>
  20. </body>
  21. </html>

HTML 内容至上

不要让非内容信息污染了你的 HTML。现在貌似有一种倾向:通过 HTML 来解决设计问题,这是显然是不对的。HTML 就应该只关注内容。

HTML 标签的目的,就是为了不断地展示内容信息。

  • 不要引入一些特定的 HTML 结构来解决一些视觉设计问题
  • 不要将 img 元素当做专门用来做视觉设计的元素

以下例子展示了误将 HTML 用来解决设计问题的这两种情况:

不推荐

html 代码:
  1. <!-- We should not introduce an additional element just to solve a design problem -->
  2. <span class="text-box">
  3. <span class="square"></span>
  4. See the square next to me?
  5. </span>
css 代码:
  1. .text-box > .square {
  2. display: inline-block;
  3. width: 1rem;
  4. height: 1rem;
  5. background-color: red;
  6. }

推荐

html 代码:
  1. <!-- That's clean markup! -->
  2. <span class="text-box">
  3. See the square next to me?
  4. </span>
css 代码:
  1. /* We use a :before pseudo element to solve the design problem of placing a colored square in front of the text content */
  2. .text-box:before {
  3. content: "";
  4. display: inline-block;
  5. width: 1rem;
  6. height: 1rem;
  7. background-color: red;
  8. }

图片和 SVG 图形能被引入到 HTML 中的唯一理由是它们呈现出了与内容相关的一些信息。

不推荐

html 代码:
  1. <!-- Content images should never be used for design elements! -->
  2. <span class="text-box">
  3. <img src="square.svg" alt="Square" />
  4. See the square next to me?
  5. </span>

推荐

html 代码:
  1. <!-- That's clean markup! -->
  2. <span class="text-box">
  3. See the square next to me?
  4. </span>
css 代码:
  1. /* We use a :before pseudo element with a background image to solve the problem */
  2. .text-box:before {
  3. content: "";
  4. display: inline-block;
  5. width: 1rem;
  6. height: 1rem;
  7. background: url(square.svg) no-repeat;
  8. background-size: 100%;
  9. }

Type 属性

省略样式表与脚本上的 type 属性。鉴于 HTML5 中以上两者默认的 type 值就是 text/css 和 text/javascript,所以 type 属性一般是可以忽略掉的。甚至在老旧版本的浏览器中这么做也是安全可靠的。

不推荐

html 代码:
  1. <link rel="stylesheet" href="main.css" type="text/css">
  2. <script src="main.js" type="text/javascript"></script>

推荐

html 代码:
  1. <link rel="stylesheet" href="main.css">
  2. <script src="main.js"></script>

可用性

如果 HTML5 语义化标签使用得当,许多可用性问题已经引刃而解。ARIA 规则在一些语义化的元素上可为其添上默认的可用性角色属性,使用得当的话已使网站的可用性大部分成立。假如你使用 navasidemainfooter 等元素,ARIA 规则会在其上应用一些关联的默认值。
更多细节可参考 ARIA specification

另外一些角色属性则能够用来呈现更多可用性情景(i.e. role="tab")。


Tab Index 在可用性上的运用

检查文档中的 tab 切换顺序并传值给元素上的 tabindex,这可以依据元素的重要性来重新排列其 tab 切换顺序。你可以设置 tabindex="-1" 在任何元素上来禁用其 tab 切换。

当你在一个默认不可聚焦的元素上增加了功能,你应该总是为其加上 tabindex 属性使其变为可聚焦状态,而且这也会激活其 CSS 的伪类 :focus。选择合适的 tabindex 值,或是直接使用 tabindex="0" 将元素们组织成同一 tab 顺序水平,并强制干预其自然阅读顺序。


微格式在 SEO 和可用性上的运用

如果 SEO 和可用性环境条件允许的话,建议考虑采用微格式。微格式是通过在元素标签上申明一系列特定数据来达成特定语义的方法。

谷歌、微软和雅虎对如何使用这些额外的数据一定程度上的达成一致,如果正确的使用,这将给搜索引擎优化带来巨大的好处。

你可以访问 schema.org 获得更多内容细节。

看一个电影网站的简单例子:

不带微格式

html 代码:
  1. <div>
  2. <h1>Avatar</h1>
  3. <span>Director: James Cameron (born August 16, 1954)</span>
  4. <span>Science fiction</span>
  5. <a href="../movies/avatar-theatrical-trailer.html">Trailer</a>
  6. </div>

带有微格式

html 代码:
  1. <div itemscope itemtype ="http://schema.org/Movie">
  2. <h1 itemprop="name">Avatar</h1>
  3. <div itemprop="director" itemscope itemtype="http://schema.org/Person">
  4. Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954)</span>
  5. </div>
  6. <span itemprop="genre">Science fiction</span>
  7. <a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
  8. </div>

ID 和锚点

通常一个比较好的做法是将页面内所有的头部标题元素都加上 ID. 这样做,页面 URL 的 hash 中带上对应的 ID 名称,即形成描点,方便跳转至对应元素所处位置。

打个比方,当你在浏览器中输入 URL http://your-site.com/about#best-practices,浏览器将定位至以下 H3 上。

html 代码:
  1. <h3 id="best-practices">Best practices</h3>

格式化规则

在每一个块状元素,列表元素和表格元素后,加上一新空白行,并对其子孙元素进行缩进。内联元素写在一行内,块状元素还有列表和表格要另起一行。

(如果由于换行的空格引发了不可预计的问题,那将所有元素并入一行也是可以接受的,格式警告总好过错误警告)。

推荐

html 代码:
  1. <blockquote>
  2. <p><em>Space</em>, the final frontier.</p>
  3. </blockquote>
  4.  
  5. <ul>
  6. <li>Moe</li>
  7. <li>Larry</li>
  8. <li>Curly</li>
  9. </ul>
  10.  
  11. <table>
  12. <thead>
  13. <tr>
  14. <th scope="col">Income</th>
  15. <th scope="col">Taxes</th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. <tr>
  20. <td>$ 5.00</td>
  21. <td>$ 4.50</td>
  22. </tr>
  23. </tbody>
  24. </table>

HTML 引号

使用双引号(“”) 而不是单引号(”) 。

不推荐

html 代码:
  1. <div class='news-article'></div>

推荐

html 代码:
  1. <div class="news-article"></div>

[1]: 此处的空白元素指的是以下元素:areabasebrcolcommandembedhrimginputkeygenlinkmetaparamsourcetrackwbr



JavaScript 规范


全局命名空间污染与 IIFE

总是将代码包裹成一个 IIFE(Immediately-Invoked Function Expression),用以创建独立隔绝的定义域。这一举措可防止全局命名空间被污染。

IIFE 还可确保你的代码不会轻易被其它全局命名空间里的代码所修改(i.e. 第三方库,window 引用,被覆盖的未定义的关键字等等)。

不推荐

js 代码:
  1. var x = 10,
  2. y = 100;
  3.  
  4. // Declaring variables in the global scope is resulting in global scope pollution. All variables declared like this
  5. // will be stored in the window object. This is very unclean and needs to be avoided.
  6. console.log(window.x + ' ' + window.y);

推荐

js 代码:
  1. // We declare a IIFE and pass parameters into the function that we will use from the global space
  2. (function(log, w, undefined){
  3. 'use strict';
  4.  
  5. var x = 10,
  6. y = 100;
  7.  
  8. // Will output 'true true'
  9. log((w.x === undefined) + ' ' + (w.y === undefined));
  10.  
  11. }(window.console.log, window));

IIFE(立即执行的函数表达式)

无论何时,想要创建一个新的封闭的定义域,那就用 IIFE。它不仅避免了干扰,也使得内存在执行完后立即释放。

所有脚本文件建议都从 IIFE 开始。

立即执行的函数表达式的执行括号应该写在外包括号内。虽然写在内还是写在外都是有效的,但写在内使得整个表达式看起来更像一个整体,因此推荐这么做。

不推荐

js 代码:
  1. (function(){})();

推荐

js 代码:
  1. (function(){}());

so,用下列写法来格式化你的 IIFE 代码:

js 代码:
  1. (function(){
  2. 'use strict';
  3.  
  4. // Code goes here
  5.  
  6. }());

如果你想引用全局变量或者是外层 IIFE 的变量,可以通过下列方式传参:

js 代码:
  1. (function($, w, d){
  2. 'use strict';
  3.  
  4. $(function() {
  5. w.alert(d.querySelectorAll('div').length);
  6. });
  7. }(jQuery, window, document));

严格模式

ECMAScript 5 严格模式可在整个脚本或独个方法内被激活。它对应不同的 javascript 语境会做更加严格的错误检查。严格模式也确保了 javascript 代码更加的健壮,运行的也更加快速。

严格模式会阻止使用在未来很可能被引入的预留关键字。

你应该在你的脚本中启用严格模式,最好是在独立的 IIFE 中应用它。避免在你的脚本第一行使用它而导致你的所有脚本都启动了严格模式,这有可能会引发一些第三方类库的问题。

不推荐

js 代码:
  1. // Script starts here
  2. 'use strict';
  3.  
  4. (function(){
  5.  
  6. // Your code starts here
  7.  
  8. }());

推荐

js 代码:
  1. (function(){
  2. 'use strict';
  3.  
  4. // Your code starts here
  5.  
  6. }());

变量声明

总是使用 var 来声明变量。如不指定 var,变量将被隐式地声明为全局变量,这将对变量难以控制。如果没有声明,变量处于什么定义域就变得不清(可以是在 Document 或 Window 中,也可以很容易地进入本地定义域)。所以,请总是使用 var 来声明变量。

采用严格模式带来的好处是,当你手误输入错误的变量名时,它可以通过报错信息来帮助你定位错误出处。

不推荐

js 代码:
  1. x = 10;
  2. y = 100;

推荐

js 代码:
  1. var x = 10,
  2. y = 100;

理解 JavaScript 的定义域和定义域提升

在 JavaScript 中变量和方法定义会自动提升到执行之前。JavaScript 只有 function 级的定义域,而无其他很多编程语言中的块定义域,所以使得你在某一 function 内的某语句和循环体中定义了一个变量,此变量可作用于整个 function 内,而不仅仅是在此语句或循环体中,因为它们的声明被 JavaScript 自动提升了。

我们通过例子来看清楚这到底是怎么一回事:

原 function

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var a = 10;
  5.  
  6. for(var i = 0; i < a; i++) {
  7. var b = i * i;
  8. log(b);
  9. }
  10.  
  11. if(a === 10) {
  12. var f = function() {
  13. log(a);
  14. };
  15. f();
  16. }
  17.  
  18. function x() {
  19. log('Mr. X!');
  20. }
  21. x();
  22.  
  23. }(window.console.log));

被 JS 提升过后

js 代码:
  1. (function(log){
  2. 'use strict';
  3. // All variables used in the closure will be hoisted to the top of the function
  4. var a,
  5. i,
  6. b,
  7. f;
  8. // All functions in the closure will be hoisted to the top
  9. function x() {
  10. log('Mr. X!');
  11. }
  12.  
  13. a = 10;
  14.  
  15. for(i = 0; i < a; i++) {
  16. b = i * i;
  17. log(b);
  18. }
  19.  
  20. if(a === 10) {
  21. // Function assignments will only result in hoisted variables but the function body will not be hoisted
  22. // Only by using a real function declaration the whole function will be hoisted with its body
  23. f = function() {
  24. log(a);
  25. };
  26. f();
  27. }
  28.  
  29. x();
  30.  
  31. }(window.console.log));

根据以上提升过程,你是否可理解以下代码?

有效代码

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var a = 10;
  5.  
  6. i = 5;
  7.  
  8. x();
  9.  
  10. for(var i; i < a; i++) {
  11. log(b);
  12. var b = i * i;
  13. }
  14.  
  15. if(a === 10) {
  16. f = function() {
  17. log(a);
  18. };
  19. f();
  20.  
  21. var f;
  22. }
  23.  
  24. function x() {
  25. log('Mr. X!');
  26. }
  27.  
  28. }(window.console.log));

正如你所看到的这段令人充满困惑与误解的代码导致了出人意料的结果。只有良好的声明习惯,也就是下一章节我们要提到的声明规则,才能尽可能的避免这类错误风险。


提升声明

为避免上一章节所述的变量和方法定义被自动提升造成误解,把风险降到最低,我们应该手动地显示地去声明变量与方法。也就是说,所有的变量以及方法,应当定义在 function 内的首行。

只用一个 var 关键字声明,多个变量用逗号隔开。

不推荐

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var a = 10;
  5. var b = 10;
  6.  
  7. for(var i = 0; i < 10; i++) {
  8. var c = a * b * i;
  9. }
  10.  
  11. function f() {
  12.  
  13. }
  14.  
  15. var d = 100;
  16. var x = function() {
  17. return d * d;
  18. };
  19. log(x());
  20.  
  21. }(window.console.log));

推荐

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var a = 10,
  5. b = 10,
  6. i,
  7. c,
  8. d,
  9. x;
  10.  
  11. function f() {
  12.  
  13. }
  14.  
  15. for(i = 0; i < 10; i++) {
  16. c = a * b * i;
  17. }
  18.  
  19.  
  20.  
  21. d = 100;
  22. x = function() {
  23. return d * d;
  24. };
  25. log(x());
  26.  
  27. }(window.console.log));

把赋值尽量写在变量申明中。

不推荐

js 代码:
  1. var a,
  2. b,
  3. c;
  4.  
  5. a = 10;
  6. b = 10;
  7. c = 100;

推荐

js 代码:
  1. var a = 10,
  2. b = 10,
  3. c = 100;

总是使用带类型判断的比较判断

总是使用 === 精确的比较操作符,避免在判断的过程中,由 JavaScript 的强制类型转换所造成的困扰。

如果你使用 === 操作符,那比较的双方必须是同一类型为前提的条件下才会有效。

如果你想了解更多关于强制类型转换的信息,你可以读一读 Dmitry Soshnikov 的这篇文章。

在只使用 == 的情况下,JavaScript 所带来的强制类型转换使得判断结果跟踪变得复杂,下面的例子可以看出这样的结果有多怪了:

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. log('0' == 0); // true
  5. log('' == false); // true
  6. log('1' == true); // true
  7. log(null == undefined); // true
  8.  
  9. var x = {
  10. valueOf: function() {
  11. return 'X';
  12. }
  13. };
  14.  
  15. log(x == 'X');
  16.  
  17. }(window.console.log));

明智地使用真假判断

当我们在一个 if 条件语句中使用变量或表达式时,会做真假判断。if(a == true) 是不同于 if(a)的。后者的判断比较特殊,我们称其为真假判断。这种判断会通过特殊的操作将其转换为 true 或 false,下列表达式统统返回 false:false0undefinednullNaN''(空字符串).

这种真假判断在我们只求结果而不关心过程的情况下,非常的有帮助。

以下示例展示了真假判断是如何工作的:

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. function logTruthyFalsy(expr) {
  5. if(expr) {
  6. log('truthy');
  7. } else {
  8. log('falsy');
  9. }
  10. }
  11.  
  12. logTruthyFalsy(true); // truthy
  13. logTruthyFalsy(1); // truthy
  14. logTruthyFalsy({}); // truthy
  15. logTruthyFalsy([]); // truthy
  16. logTruthyFalsy('0'); // truthy
  17.  
  18. logTruthyFalsy(false); // falsy
  19. logTruthyFalsy(0); // falsy
  20. logTruthyFalsy(undefined); // falsy
  21. logTruthyFalsy(null); // falsy
  22. logTruthyFalsy(NaN); // falsy
  23. logTruthyFalsy(''); // falsy
  24.  
  25. }(window.console.log));

变量赋值时的逻辑操作

逻辑操作符 || 和 && 也可被用来返回布尔值。如果操作对象为非布尔对象,那每个表达式将会被自左向右地做真假判断。基于此操作,最终总有一个表达式被返回回来。这在变量赋值时,是可以用来简化你的代码的。

不推荐

js 代码:
  1. if(!x) {
  2. if(!y) {
  3. x = 1;
  4. } else {
  5. x = y;
  6. }
  7. }

推荐

js 代码:
  1. x = x || y || 1;

这一小技巧经常用来给方法设定默认的参数。

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. function multiply(a, b) {
  5. a = a || 1;
  6. b = b || 1;
  7.  
  8. log('Result ' + a * b);
  9. }
  10.  
  11. multiply(); // Result 1
  12. multiply(10); // Result 10
  13. multiply(3, NaN); // Result 3
  14. multiply(9, 5); // Result 45
  15.  
  16. }(window.console.log));

分号

总是使用分号,因为隐式的代码嵌套会引发难以察觉的问题。当然我们更要从根本上来杜绝这些问题[1]。以下几个示例展示了缺少分号的危害:

js 代码:
  1. // 1.
  2. MyClass.prototype.myMethod = function() {
  3. return 42;
  4. } // No semicolon here.
  5.  
  6. (function() {
  7. // Some initialization code wrapped in a function to create a scope for locals.
  8. })();
  9.  
  10.  
  11. var x = {
  12. 'i': 1,
  13. 'j': 2
  14. } // No semicolon here.
  15.  
  16. // 2. Trying to do one thing on Internet Explorer and another on Firefox.
  17. // I know you'd never write code like this, but throw me a bone.
  18. [ffVersion, ieVersion][isIE]();
  19.  
  20.  
  21. var THINGS_TO_EAT = [apples, oysters, sprayOnCheese] // No semicolon here.
  22.  
  23. // 3. conditional execution a la bash
  24. -1 == resultOfOperation() || die();

So what happens?

  1. JavaScript 错误 —— 首先返回 42 的那个 function 被第二个 function 当中参数传入调用,接着数字 42 也被“调用”而导致出错。
  2. 八成你会得到 ‘no such property in undefined’ 的错误提示,因为在真实环境中的调用是这个样子:x[ffVersion, ieVersion][isIE]().
  3. die 总是被调用。因为数组减 1 的结果是 NaN,它不等于任何东西(无论 resultOfOperation 是否返回 NaN)。所以最终的结果是 die() 执行完所获得值将赋给 THINGS_TO_EAT.

Why?

JavaScript 中语句要以分号结束,否则它将会继续执行下去,不管换不换行。以上的每一个示例中,函数声明或对象或数组,都变成了在一句语句体内。要知道闭合圆括号并不代表语句结束,JavaScript 不会终结语句,除非它的下一个 token 是一个中缀符[2] 或者是圆括号操作符。

这真是让人大吃一惊,所以乖乖地给语句末加上分号吧。

澄清:分号与函数

分号需要用在表达式的结尾,而并非函数声明的结尾。区分它们最好的例子是:

js 代码:
  1. var foo = function() {
  2. return true;
  3. }; // semicolon here.
  4.  
  5. function foo() {
  6. return true;
  7. } // no semicolon here.

嵌套函数

嵌套函数是非常有用的,比如用在持续创建和隐藏辅助函数的任务中。你可以非常自由随意地使用它们。


语句块内的函数声明

切勿在语句块内声明函数,在 ECMAScript 5 的严格模式下,这是不合法的。函数声明应该在定义域的顶层。但在语句块内可将函数申明转化为函数表达式赋值给变量。

不推荐

js 代码:
  1. if (x) {
  2. function foo() {}
  3. }

推荐

js 代码:
  1. if (x) {
  2. var foo = function() {};
  3. }

异常

基本上你无法避免出现异常,特别是在做大型开发时(使用应用开发框架等等)。

在没有自定义异常的情况下,从有返回值的函数中返回错误信息一定非常的棘手,更别提多不优雅了。不好的解决方案包括了传第一个引用类型来接纳错误信息,或总是返回一个对象列表,其中包含着可能的错误对象。以上方式基本上是比较简陋的异常处理方式。适时可做自定义异常处理。

在复杂的环境中,你可以考虑抛出对象而不仅仅是字符串(默认的抛出值)。

js 代码:
  1. if(name === undefined) {
  2. throw {
  3. name: 'System Error',
  4. message: 'A name should always be specified!'
  5. }
  6. }

标准特性

总是优先考虑使用标准特性。为了最大限度地保证扩展性与兼容性,总是首选标准的特性,而不是非标准的特性(例如:首选 string.charAt(3) 而不是 string[3];首选 DOM 的操作方法来获得元素引用,而不是某一应用特定的快捷方法)。


简易的原型继承

如果你想在 JavaScript 中继承你的对象,请遵循一个简易的模式来创建此继承。如果你预计你会遇上复杂对象的继承,那可以考虑采用一个继承库,比如 Proto.js by Axel Rauschmayer.

简易继承请用以下方式:

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. // Constructor function
  5. function Apple(name) {
  6. this.name = name;
  7. }
  8. // Defining a method of apple
  9. Apple.prototype.eat = function() {
  10. log('Eating ' + this.name);
  11. };
  12.  
  13. // Constructor function
  14. function GrannySmithApple() {
  15. // Invoking parent constructor
  16. Apple.prototype.constructor.call(this, 'Granny Smith');
  17. }
  18. // Set parent prototype while creating a copy with Object.create
  19. GrannySmithApple.prototype = Object.create(Apple.prototype);
  20. // Set constructor to the sub type, otherwise points to Apple
  21. GrannySmithApple.prototype.constructor = GrannySmithApple;
  22.  
  23. // Calling a super method
  24. GrannySmithApple.prototype.eat = function() {
  25. // Be sure to apply it onto our current object with call(this)
  26. Apple.prototype.eat.call(this);
  27.  
  28. log('Poor Grany Smith');
  29. };
  30.  
  31. // Instantiation
  32. var apple = new Apple('Test Apple');
  33. var grannyApple = new GrannySmithApple();
  34.  
  35. log(apple.name); // Test Apple
  36. log(grannyApple.name); // Granny Smith
  37.  
  38. // Instance checks
  39. log(apple instanceof Apple); // true
  40. log(apple instanceof GrannySmithApple); // false
  41.  
  42. log(grannyApple instanceof Apple); // true
  43. log(grannyApple instanceof GrannySmithApple); // true
  44.  
  45. // Calling method that calls super method
  46. grannyApple.eat(); // Eating Granny Smith\nPoor Grany Smith
  47.  
  48. }(window.console.log));

使用闭包

闭包的创建也许是 JS 最有用也是最易被忽略的能力了。关于闭包如何工作的合理解释。


切勿在循环中创建函数

在简单的循环语句中加入函数是非常容易形成闭包而带来隐患的。下面的例子就是一个典型的陷阱:

不推荐

js 代码:
  1. (function(log, w){
  2. 'use strict';
  3.  
  4. // numbers and i is defined in the current function closure
  5. var numbers = [1, 2, 3],
  6. i;
  7.  
  8. for(i = 0; i < numbers.length; i++) {
  9. w.setTimeout(function() {
  10. // At the moment when this gets executed the i variable, coming from the outer function scope
  11. // is set to 3 and the current program is alerting the message 3 times
  12. // 'Index 3 with number undefined
  13. // If you understand closures in javascript you know how to deal with those cases
  14. // It's best to just avoid functions / new closures in loops as this prevents those issues
  15.  
  16. w.alert('Index ' + i + ' with number ' + numbers[i]);
  17. }, 0);
  18. }
  19.  
  20. }(window.console.log, window));

接下来的改进虽然已经解决了上述例子中的问题或 bug,但还是违反了不在循环中创建函数或闭包的原则。

不推荐

js 代码:
  1. (function(log, w){
  2. 'use strict';
  3.  
  4. // numbers and i is defined in the current function closure
  5. var numbers = [1, 2, 3],
  6. i;
  7.  
  8. for(i = 0; i < numbers.length; i++) { // Creating a new closure scope with an IIFE solves the problem // The delayed function will use index and number which are // in their own closure scope (one closure per loop iteration). // --- // Still this is not recommended as we violate our rule to not // create functions within loops and we are creating two! (function(index, number){ w.setTimeout(function() { // Will output as expected 0 > 1, 1 > 2, 2 > 3
  9. w.alert('Index ' + index + ' with number ' + number);
  10. }, 0);
  11. }(i, numbers[i]));
  12. }
  13.  
  14. }(window.console.log, window));

接下来的改进已解决问题,而且也遵循了规范。可是,你会发现看上去似乎过于复杂繁冗了,应该会有更好的解决方案吧。

不完全推荐

js 代码:
  1. (function(log, w){
  2. 'use strict';
  3.  
  4. // numbers and i is defined in the current function closure
  5. var numbers = [1, 2, 3],
  6. i;
  7.  
  8. // Create a function outside of the loop that will accept arguments to create a
  9. // function closure scope. This function will return a function that executes in this
  10. // closure parent scope.
  11. function alertIndexWithNumber(index, number) {
  12. return function() {
  13. w.alert('Index ' + index + ' with number ' + number);
  14. };
  15. }
  16.  
  17. // First parameter is a function call that returns a function.
  18. // ---
  19. // This solves our problem and we don't create a function inside our loop
  20. for(i = 0; i < numbers.length; i++) {
  21. w.setTimeout(alertIndexWithNumber(i, numbers[i]), 0);
  22. }
  23.  
  24. }(window.console.log, window));

将循环语句转换为函数执行的方式问题能得到立马解决,每一次循环都会对应地创建一次闭包。函数式的风格更加值得推荐,而且看上去也更加地自然和可预料。

推荐

js 代码:
  1. (function(log, w){
  2. 'use strict';
  3.  
  4. // numbers and i is defined in the current function closure
  5. var numbers = [1, 2, 3],
  6. i;
  7.  
  8. numbers.forEach(function(number, index) {
  9. w.setTimeout(function() {
  10. w.alert('Index ' + index + ' with number ' + number);
  11. }, 0);
  12. });
  13.  
  14. }(window.console.log, window));

eval 函数(魔鬼)

eval() 不但混淆语境还很危险,总会有比这更好、更清晰、更安全的另一种方案来写你的代码,因此尽量不要使用 evil 函数。


this 关键字

只在对象构造器、方法和在设定的闭包中使用 this 关键字。this 的语义在此有些误导。它时而指向全局对象(大多数时),时而指向调用者的定义域(在 eval 中),时而指向 DOM 树中的某一节点(当用事件处理绑定到 HTML 属性上时),时而指向一个新创建的对象(在构造器中),还时而指向其它的一些对象(如果函数被 call() 和 apply() 执行和调用时)。

正因为它是如此容易地被搞错,请限制它的使用场景:

  • 在构造函数中
  • 在对象的方法中(包括由此创建出的闭包内)

首选函数式风格

函数式编程让你可以简化代码并缩减维护成本,因为它容易复用,又适当地解耦和更少的依赖。

接下来的例子中,在一组数字求和的同一问题上,比较了两种解决方案。第一个例子是经典的程序处理,而第二个例子则是采用了函数式编程和 ECMA Script 5.1 的数组方法。

例外:往往在重代码性能轻代码维护的情况之下,要选择最优性能的解决方案而非维护性高的方案(比如用简单的循环语句代替 forEach)。

不推荐

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var arr = [10, 3, 7, 9, 100, 20],
  5. sum = 0,
  6. i;
  7.  
  8.  
  9. for(i = 0; i < arr.length; i++) {
  10. sum += arr[i];
  11. }
  12.  
  13. log('The sum of array ' + arr + ' is: ' + sum)
  14.  
  15. }(window.console.log));

推荐

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var arr = [10, 3, 7, 9, 100, 20];
  5.  
  6. var sum = arr.reduce(function(prevValue, currentValue) {
  7. return prevValue + currentValue;
  8. }, 0);
  9.  
  10. log('The sum of array ' + arr + ' is: ' + sum);
  11.  
  12. }(window.console.log));

另一个例子通过某一规则对一个数组进行过滤匹配来创建一个新的数组。

不推荐

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var numbers = [11, 3, 7, 9, 100, 20, 14, 10],
  5. numbersGreaterTen = [],
  6. i;
  7.  
  8.  
  9. for(i = 0; i < numbers.length; i++) { if(numbers[i] > 10) {
  10. numbersGreaterTen.push(numbers[i]);
  11. }
  12. }
  13.  
  14. log('From the list of numbers ' + numbers + ' only ' + numbersGreaterTen + ' are greater than ten');
  15.  
  16. }(window.console.log));

推荐

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. var numbers = [11, 3, 7, 9, 100, 20, 14, 10];
  5.  
  6. var numbersGreaterTen = numbers.filter(function(element) {
  7. return element > 10;
  8. });
  9.  
  10. log('From the list of numbers ' + numbers + ' only ' + numbersGreaterTen + ' are greater than ten');
  11.  
  12. }(window.console.log));

使用 ECMA Script 5

建议使用 ECMA Script 5 中新增的语法糖和函数。这将简化你的程序,并让你的代码更加灵活和可复用。


数组和对象的属性迭代

用 ECMA5 的迭代方法来迭代数组。使用 Array.forEach 或者如果你要在特殊场合下中断迭代,那就用 Array.every

js 代码:
  1. (function(log){
  2. 'use strict';
  3.  
  4. // Iterate over an array and break at a certain condition
  5. [1, 2, 3, 4, 5].every(function(element, index, arr) {
  6. log(element + ' at index ' + index + ' in array ' + arr);
  7.  
  8. if(index !== 5) {
  9. return true;
  10. }
  11. });
  12.  
  13. // Defining a simple javascript object
  14. var obj = {
  15. a: 'A',
  16. b: 'B',
  17. 'c-d-e': 'CDE'
  18. };
  19.  
  20. // Iterating over the object keys
  21. Object.keys(obj).forEach(function(element, index, arr) {
  22. log('Key ' + element + ' has value ' + obj[element]);
  23. });
  24.  
  25. }(window.console.log));

不要使用 switch

switch 在所有的编程语言中都是个非常错误的难以控制的语句,建议用 if else 来替换它。


数组和对象字面量

用数组和对象字面量来代替数组和对象构造器。数组构造器很容易让人在它的参数上犯错。

不推荐

js 代码:
  1. // Length is 3.
  2. var a1 = new Array(x1, x2, x3);
  3.  
  4. // Length is 2.
  5. var a2 = new Array(x1, x2);
  6.  
  7. // If x1 is a number and it is a natural number the length will be x1.
  8. // If x1 is a number but not a natural number this will throw an exception.
  9. // Otherwise the array will have one element with x1 as its value.
  10. var a3 = new Array(x1);
  11.  
  12. // Length is 0.
  13. var a4 = new Array();

正因如此,如果将代码传参从两个变为一个,那数组很有可能发生意料不到的长度变化。为避免此类怪异状况,请总是采用更多可读的数组字面量。

推荐

js 代码:
  1. var a = [x1, x2, x3];
  2. var a2 = [x1, x2];
  3. var a3 = [x1];
  4. var a4 = [];

对象构造器不会有类似的问题,但是为了可读性和统一性,我们应该使用对象字面量。

不推荐

js 代码:
  1. var o = new Object();
  2.  
  3. var o2 = new Object();
  4. o2.a = 0;
  5. o2.b = 1;
  6. o2.c = 2;
  7. o2['strange key'] = 3;

应该写成这样:

推荐

js 代码:
  1. var o = {};
  2.  
  3. var o2 = {
  4. a: 0,
  5. b: 1,
  6. c: 2,
  7. 'strange key': 3
  8. };

修改内建对象的原型链

修改内建的诸如 Object.prototype 和 Array.prototype 是被严厉禁止的。修改其它的内建对象比如 Function.prototype,虽危害没那么大,但始终还是会导致在开发过程中难以 debug 的问题,应当也要避免。


自定义 toString() 方法

你可以通过自定义 toString() 来控制对象字符串化。这很好,但你必须保证你的方法总是成功并不会有其它副作用。如果你的方法达不到这样的标准,那将会引发严重的问题。如果 toString() 调用了一个方法,这个方法做了一个断言[3] ,当断言失败,它可能会输出它所在对象的名称,当然对象也需要调用 toString()


圆括号

一般在语法和语义上真正需要时才谨慎地使用圆括号。不要用在一元操作符上,例如 deletetypeof 和 void,或在关键字之后,例如 returnthrowcasenew 等。


字符串

统一使用单引号(‘),不使用双引号(“)。这在创建 HTML 字符串非常有好处:

js 代码:
  1. var msg = 'This is some HTML <div class="makes-sense"></div>';

三元条件判断(if 的快捷方法)

用三元操作符分配或返回语句。在比较简单的情况下使用,避免在复杂的情况下使用。没人愿意用 10 行三元操作符把自己的脑子绕晕。

不推荐

js 代码:
  1. if(x === 10) {
  2. return 'valid';
  3. } else {
  4. return 'invalid';
  5. }

推荐

js 代码:
  1. return x === 10 ? 'valid' : 'invalid';

[1]:作者指的是采用严格规范的语句写法,从根本上杜绝由分号缺失而引起的代码歧义。

[2]:中缀符,指的是像 x + y 中的 +

[3]:断言一般指程序员在测试测序时的假设,一般是一些布尔表达式,当返回是 true 时,断言为真,代码运行会继续进行;如果条件判断为 false,代码运行停止,你的应用被终止。


CSS and Sass (SCSS) style rules


ID and class naming

ID和class(类)名总是使用可以反应元素目的和用途的名称,或其他通用名称。代替表象和晦涩难懂的名称。

应该首选具体和反映元素目的的名称,因为这些是最可以理解的,而且发生变化的可能性最小。

通用名称只是多个元素的备用名,他们兄弟元素之间是一样的,没有特别意义。
区分他们,使他们具有特殊意义,通常需要为“帮手”。

尽管class(类)名和ID 的语义化对于计算机解析来说没有什么实际的意义,
语义化的名称 通常是正确的选择,因为它们所代表的信息含义,不包含表现的限制。

不推荐

css 代码:
  1. .fw-800 {
  2. font-weight: 800;
  3. }
  4.  
  5. .red {
  6. color: red;
  7. }

推荐

css 代码:
  1. .heavy {
  2. font-weight: 800;
  3. }
  4.  
  5. .important {
  6. color: red;
  7. }

合理的避免使用ID

一般情况下ID不应该被应用于样式。
ID的样式不能被复用并且每个页面中你只能使用一次ID。
使用ID唯一有效的是确定网页或整个站点中的位置。
尽管如此,你应该始终考虑使用class,而不是id,除非只使用一次。

不推荐

css 代码:
  1. #content .title {
  2. font-size: 2em;
  3. }

推荐

css 代码:
  1. .content .title {
  2. font-size: 2em;
  3. }

另一个反对使用ID的观点是含有ID选择器权重很高。
一个只包含一个ID选择器权重高于包含1000个class(类)名的选择器,这使得它很奇怪。

css 代码:
  1. // 这个选择器权重高于下面的选择器
  2. #content .title {
  3. color: red;
  4. }
  5.  
  6. // than this selector!
  7. html body div.content.news-content .title.content-title.important {
  8. color: blue;
  9. }

CSS选择器中避免标签名

当构建选择器时应该使用清晰, 准确和有语义的class(类)名。不要使用标签选择器。 如果你只关心你的class(类)名
,而不是你的代码元素,这样会更容易维护。

从分离的角度考虑,在表现层中不应该分配html标记/语义。
它可能是一个有序列表需要被改成一个无序列表,或者一个div将被转换成article。
如果你只使用具有实际意义的class(类)名,
并且不使用元素选择器,那么你只需要改变你的html标记,而不用改动你的CSS。

不推荐

css 代码:
  1. div.content > header.content-header > h2.title {
  2. font-size: 2em;
  3. }

推荐

css 代码:
  1. .content > .content-header > .title {
  2. font-size: 2em;
  3. }

尽可能的精确

很多前端开发人员写选择器链的时候不使用 直接子选择器(注:直接子选择器和后代选择器的区别)。
有时,这可能会导致疼痛的设计问题并且有时候可能会很耗性能。
然而,在任何情况下,这是一个非常不好的做法。
如果你不写很通用的,需要匹配到DOM末端的选择器, 你应该总是考虑直接子选择器。

考虑下面的DOM:

css 代码:
  1. <article class="content news-content">
  2. <span class="title">News event</span>
  3. <div class="content-body">
  4. <div class="title content-title">
  5. Check this out
  6. </div>
  7.  
  8. <p>This is a news article content</p>
  9.  
  10. <div class="teaser">
  11. <div class="title">Buy this</div>
  12. <div class="teaser-content">Yey!</div>
  13. </div>
  14. </div>
  15. </article>

下面的CSS将应用于有title类的全部三个元素。
然后,要解决content类下的title类 和 teaser类下的title类下不同的样式,这将需要更精确的选择器再次重写他们的样式。

不推荐

css 代码:
  1. .content .title {
  2. font-size: 2rem;
  3. }

推荐

css 代码:
  1. .content > .title {
  2. font-size: 2rem;
  3. }
  4.  
  5. .content > .content-body > .title {
  6. font-size: 1.5rem;
  7. }
  8.  
  9. .content > .content-body > .teaser > .title {
  10. font-size: 1.2rem;
  11. }

缩写属性

CSS提供了各种缩写属性(如 font 字体)应该尽可能使用,即使在只设置一个值的情况下。

使用缩写属性对于代码效率和可读性是有很有用的。

不推荐

css 代码:
  1. border-top-style: none;
  2. font-family: palatino, georgia, serif;
  3. font-size: 100%;
  4. line-height: 1.6;
  5. padding-bottom: 2em;
  6. padding-left: 1em;
  7. padding-right: 1em;
  8. padding-top: 0;

推荐

css 代码:
  1. border-top: 0;
  2. font: 100%/1.6 palatino, georgia, serif;
  3. padding: 0 1em 2em;

0 和 单位

省略“0”值后面的单位。不要在0值后面使用单位,除非有值。

不推荐

css 代码:
  1. padding-bottom: 0px;
  2. margin: 0em;

推荐

css 代码:
  1. padding-bottom: 0;
  2. margin: 0;

十六进制表示法

在可能的情况下,使用3个字符的十六进制表示法。
颜色值允许这样表示,
3个字符的十六进制表示法更简短。

始终使用小写的十六进制数字。

不推荐

css 代码:
  1. color: #FF33AA;

推荐

css 代码:
  1. color: #f3a;

ID 和 Class(类) 名的分隔符

使用连字符(中划线)分隔ID和Class(类)名中的单词。为了增强课理解性,在选择器中不要使用除了连字符(中划线)以为的任何字符(包括没有)来连接单词和缩写。

另外,作为该标准,预设属性选择器能识别连字符(中划线)作为单词[attribute|=value]的分隔符,
所以最好的坚持使用连字符作为分隔符。

不推荐

css 代码:
  1. .demoimage {}
  2. .error_status {}

推荐

css 代码:
  1. #video-id {}
  2. .ads-sample {}

Hacks

避免用户代理检测以及CSS“hacks” – 首先尝试不同的方法。通过用户代理检测或特殊的CSS滤镜,变通的方法和 hacks 很容易解决样式差异。为了达到并保持一个有效的和可管理的代码库,这两种方法都应该被认为是最后的手段。换句话说,从长远来看,用户代理检测和hacks
会伤害项目,作为项目往往应该采取阻力最小的途径。也就是说,轻易允许使用用户代理检测和hacks 以后将过于频繁。


声明顺序

这是一个选择器内书写CSS属性顺序的大致轮廓。这是为了保证更好的可读性和可扫描重要。

作为最佳实践,我们应该遵循以下顺序(应该按照下表的顺序):

  1. 结构性属性:
    1. display
    2. position, left, top, right etc.
    3. overflow, float, clear etc.
    4. margin, padding
  2. 表现性属性:
    1. background, border etc.
    2. font, text

不推荐

css 代码:
  1. .box {
  2. font-family: 'Arial', sans-serif;
  3. border: 3px solid #ddd;
  4. left: 30%;
  5. position: absolute;
  6. text-transform: uppercase;
  7. background-color: #eee;
  8. right: 30%;
  9. isplay: block;
  10. font-size: 1.5rem;
  11. overflow: hidden;
  12. padding: 1em;
  13. margin: 1em;
  14. }

推荐

css 代码:
  1. .box {
  2. display: block;
  3. position: absolute;
  4. left: 30%;
  5. right: 30%;
  6. overflow: hidden;
  7. margin: 1em;
  8. padding: 1em;
  9. background-color: #eee;
  10. border: 3px solid #ddd;
  11. font-family: 'Arial', sans-serif;
  12. font-size: 1.5rem;
  13. text-transform: uppercase;
  14. }

声明结束

为了保证一致性和可扩展性,每个声明应该用分号结束,每个声明换行。

不推荐

css 代码:
  1. .test {
  2. display: block; height: 100px
  3. }

推荐

css 代码:
  1. .test {
  2. display: block;
  3. height: 100px;
  4. }

属性名结束

属性名的冒号后使用一个空格。出于一致性的原因,
属性和值(但属性和冒号之间没有空格)的之间始终使用一个空格。

不推荐

css 代码:
  1. h3 {
  2. font-weight:bold;
  3. }

推荐

css 代码:
  1. h3 {
  2. font-weight: bold;
  3. }

选择器和声明分离

每个选择器和属性声明总是使用新的一行。

不推荐

css 代码:
  1. a:focus, a:active {
  2. position: relative; top: 1px;
  3. }

推荐

css 代码:
  1. h1,
  2. h2,
  3. h3 {
  4. font-weight: normal;
  5. line-height: 1.2;
  6. }

规则分隔

规则之间始终有一个空行(双换行符)分隔。

推荐

css 代码:
  1. html {
  2. background: #fff;
  3. }
  4.  
  5. body {
  6. margin: auto;
  7. width: 50%;
  8. }

CSS引号

属性选择器或属性值用双引号(””),而不是单引号(”)括起来。
URI值(url())不要使用引号。

不推荐

css 代码:
  1. @import url('//cdn.com/foundation.css');
  2.  
  3. html {
  4. font-family: 'open sans', arial, sans-serif;
  5. }
  6.  
  7. body:after {
  8. content: 'pause';
  9. }

推荐

css 代码:
  1. @import url(//cdn.com/foundation.css);
  2.  
  3. html {
  4. font-family: "open sans", arial, sans-serif;
  5. }
  6.  
  7. body:after {
  8. content: "pause";
  9. }

选择器嵌套 (SCSS)

在Sass中你可以嵌套选择器,这可以使代码变得更清洁和可读。嵌套所有的选择器,但尽量避免嵌套没有任何内容的选择器。
如果你需要指定一些子元素的样式属性,而父元素将不什么样式属性,
可以使用常规的CSS选择器链。
这将防止您的脚本看起来过于复杂。

不推荐

css 代码:
  1. // Not a good example by not making use of nesting at all
  2. .content {
  3. display: block;
  4. }
  5.  
  6. .content > .news-article > .title {
  7. font-size: 1.2em;
  8. }

不推荐

css 代码:
  1. // Using nesting is better but not in all cases
  2. // Avoid nesting when there is no attributes and use selector chains instead
  3. .content {
  4. display: block;
  5.  
  6. > .news-article {
  7. > .title {
  8. font-size: 1.2em;
  9. }
  10. }
  11. }

推荐

css 代码:
  1. // This example takes the best approach while nesting but use selector chains where possible
  2. .content {
  3. display: block;
  4.  
  5. > .news-article > .title {
  6. font-size: 1.2em;
  7. }
  8. }

嵌套中引入 空行 (SCSS)

嵌套选择器和CSS属性之间空一行。

不推荐

css 代码:
  1. .content {
  2. display: block;
  3. > .news-article {
  4. background-color: #eee;
  5. > .title {
  6. font-size: 1.2em;
  7. }
  8. > .article-footnote {
  9. font-size: 0.8em;
  10. }
  11. }
  12. }

推荐

css 代码:
  1. .content {
  2. display: block;
  3.  
  4. > .news-article {
  5. background-color: #eee;
  6.  
  7. > .title {
  8. font-size: 1.2em;
  9. }
  10.  
  11. > .article-footnote {
  12. font-size: 0.8em;
  13. }
  14. }
  15. }

上下文媒体查询(SCSS)

在Sass中,当你嵌套你的选择器时也可以使用上下文媒体查询。
在Sass中,你可以在任何给定的嵌套层次中使用媒体查询。
由此生成的CSS将被转换,这样的媒体查询将包裹选择器的形式呈现。

这技术非常方便,
有助于保持媒体查询属于的上下文。

第一种方法这可以让你先写你的手机样式,然后在任何你需要的地方
用上下文媒体查询以提供桌面样式。

不推荐

css 代码:
  1. // This mobile first example looks like plain CSS where the whole structure of SCSS is repeated
  2. // on the bottom in a media query. This is error prone and makes maintenance harder as it's not so easy to relate
  3. // the content in the media query to the content in the upper part (mobile style)
  4.  
  5. .content-page {
  6. font-size: 1.2rem;
  7.  
  8. > .main {
  9. background-color: whitesmoke;
  10.  
  11. > .latest-news {
  12. padding: 1rem;
  13.  
  14. > .news-article {
  15. padding: 1rem;
  16.  
  17. > .title {
  18. font-size: 2rem;
  19. }
  20. }
  21. }
  22.  
  23. > .content {
  24. margin-top: 2rem;
  25. padding: 1rem;
  26. }
  27. }
  28.  
  29. > .page-footer {
  30. margin-top: 2rem;
  31. font-size: 1rem;
  32. }
  33. }
  34.  
  35. @media screen and (min-width: 641px) {
  36. .content-page {
  37. font-size: 1rem;
  38.  
  39. > .main > .latest-news > .news-article > .title {
  40. font-size: 3rem;
  41. }
  42.  
  43. > .page-footer {
  44. font-size: 0.8rem;
  45. }
  46. }
  47. }

推荐

css 代码:
  1. // This is the same example as above but here we use contextual media queries in order to put the different styles
  2. // for different media into the right context.
  3.  
  4. .content-page {
  5. font-size: 1.2rem;
  6.  
  7. @media screen and (min-width: 641px) {
  8. font-size: 1rem;
  9. }
  10.  
  11. > .main {
  12. background-color: whitesmoke;
  13.  
  14. > .latest-news {
  15. padding: 1rem;
  16.  
  17. > .news-article {
  18. padding: 1rem;
  19.  
  20. > .title {
  21. font-size: 2rem;
  22.  
  23. @media screen and (min-width: 641px) {
  24. font-size: 3rem;
  25. }
  26. }
  27. }
  28. }
  29.  
  30. > .content {
  31. margin-top: 2rem;
  32. padding: 1rem;
  33. }
  34. }
  35.  
  36. > .page-footer {
  37. margin-top: 2rem;
  38. font-size: 1rem;
  39.  
  40. @media screen and (min-width: 641px) {
  41. font-size: 0.8rem;
  42. }
  43. }
  44. }

嵌套顺序和父级选择器(SCSS)

当使用Sass的嵌套功能的时候,
重要的是有一个明确的嵌套顺序,
以下内容是一个SCSS块应具有的顺序。

  1. 当前选择器的样式属性
  2. 父级选择器的伪类选择器 (:first-letter, :hover, :active etc)
  3. 伪类元素 (:before and :after)
  4. 父级选择器的声明样式 (.selected, .active, .enlarged etc.)
  5. 用Sass的上下文媒体查询
  6. 子选择器作为最后的部分

The following example should illustrate how this ordering will achieve a clear structure while making use of the Sass parent selector.

Recommended

css 代码:
  1. .product-teaser {
  2. // 1. Style attributes
  3. display: inline-block;
  4. padding: 1rem;
  5. background-color: whitesmoke;
  6. color: grey;
  7.  
  8. // 2. Pseudo selectors with parent selector
  9. &:hover {
  10. color: black;
  11. }
  12.  
  13. // 3. Pseudo elements with parent selector
  14. &:before {
  15. content: "";
  16. display: block;
  17. border-top: 1px solid grey;
  18. }
  19.  
  20. &:after {
  21. content: "";
  22. display: block;
  23. border-top: 1px solid grey;
  24. }
  25.  
  26. // 4. State classes with parent selector
  27. &.active {
  28. background-color: pink;
  29. color: red;
  30.  
  31. // 4.2. Pseuso selector in state class selector
  32. &:hover {
  33. color: darkred;
  34. }
  35. }
  36.  
  37. // 5. Contextual media queries
  38. @media screen and (max-width: 640px) {
  39. display: block;
  40. font-size: 2em;
  41. }
  42.  
  43. // 6. Sub selectors
  44. > .content > .title {
  45. font-size: 1.2em;
  46.  
  47. // 6.5. Contextual media queries in sub selector
  48. @media screen and (max-width: 640px) {
  49. letter-spacing: 0.2em;
  50. text-transform: uppercase;
  51. }
  52. }
  53. }



0 0
原创粉丝点击