[20141011]编写高质量JS代码的68个有效方法(二)

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

No.6、了解分号插入的局限性

Tips:

  1. 仅在“}”标记之前、一行的结束和程序的结束处推导分号
  2. 仅在紧接着的标记不能被解析的时候推导分号
  3. 在以(、[、+、-或/字符开头的语句前绝不能省略分号
  4. 当脚本连接的时候,在脚本之间显式的插入分号
  5. 在return、throw、break、continue、++或--的参数之前绝不能换行
  6. 分号不能作为for循环的头部和空语句的分隔符而被推导出
  7. 个人总结:尽量不要省略分号,不要让JS自动推导

分号仅在}标记之前、一个或多个换行之后和程序输入的结尾被插入,看代码:

// 能自动推导分号
function square(x){
var n = +x
return n*n
} // Error,不能自动推导
function square(x){var n = +x return n*n}

分号仅在随后的输入标记不能被解析时插入,看代码:

a=b
(f());
此时,代码等价于 ab(f());
但是:
a=b
f()
则会被解析为a=b f();

在以(、[、+、-或/字符开头的语句前,绝不能省略分号,看代码:

a=b
['a','b','c'].forEach(function(key){
console.log(key)
})
等价于
a=b['a','b','c'].forEach(function(key){
console.log(key);
}); a=1
/Error/i.test('test')
等价于
a=1/Error/i.test('test');

合并脚本时不能省略分号,看代码:

//file1.js
(function(){console.log('file1')})() //file2.js
(function(){console.log('file2')})() //合并后 --输出file1,然后报错
(function(){console.log('file1')})()(function(){console.log('file2')})()

为了防止自己写的库在合并时内其他代码干扰,所以一般写法为如下代码:

;(function(){
/*Code*/
})();

在return、throw、break、continue、++或--的参数之前绝不能换行,看代码:

a
++
b
等价于:
a;++b;

for循环中不要省略分号

//Parse Error
var total=0
for(var i=0,total=1
i<n
i++){
total*=i
}

综上,再次强调,不加分号看起来代码轻量,但稍不注意就会引起很多bug,所以,建议都加上分号,不要让JS环境自行推导

No.7、视字符串为16位的代码单元序列

待定...

No.8、尽量少用全局对象

Tips:

  1. 避免申明全局变量
  2. 尽量申明局部变量
  3. 避免对全局对象添加属性
  4. 使用全局对象来做平台特性检测

定义全局变量会污染共享的公命名空间,并可能导致意外的命名冲突。全局变量不利于模块化,因为它会导致程序中独立组件间的不必要耦合。

No.9、始终声明局部变量

Tips:

  1. 始终使用var声明新的局部变量
  2. 考虑使用lint工具来帮助检查未绑定的变量

如果存在比全局变量更麻烦的事情,那就是意外的全局变量。由于不适用var申明的变量,统统为全局变量,所以一定要使用var来定义变量,防止变量污染。

function test(){
test='test';
}
test();
window.test;// 'test'

No.10、避免使用with

Tips:

  1. 避免使用with语句
  2. 使用简短的变量名代替重复访问的对象
  3. 显式地绑定局部变量到对象属性上,而不要使用with语句隐式地绑定他们

待定...

编写高质量JS代码的68个有效方法(二)的更多相关文章

  1. 编写高质量JS代码的68个有效方法(八)

    [20141227]编写高质量JS代码的68个有效方法(八) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  2. 编写高质量JS代码的68个有效方法(七)

    [20141220]编写高质量JS代码的68个有效方法(七) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  3. 编写高质量JS代码的68个有效方法(六)

    [20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  4. 编写高质量JS代码的68个有效方法(四)

    [20141129]编写高质量JS代码的68个有效方法(四) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  5. 编写高质量JS代码的68个有效方法(三)

    [20141030]编写高质量JS代码的68个有效方法(三) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  6. JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)

    编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  7. 编写高质量JS代码的68个有效方法(十三)

    No.61.不要阻塞I/O事件队列 Tips: 异步API使用回调函数来延缓处理代价高昂的操作以避免阻塞主应用程序 JavaScript并发的接收事件,但会使用一个事件队列按序地处理事件处理程序 在应 ...

  8. 编写高质量JS代码的68个有效方法(十)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  9. 编写高质量JS代码的68个有效方法(十一)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. WizardDialog 进度条使用记录

    开发RCP的朋友们经常会使用到导航窗口, 先简单介绍一下wizardDialog,基本上他的使用方法是这样的 首先有一个WizardDialog,在dialog里面需要放一个Wizard来控制页面Wi ...

  2. unity 读取excel表 生成asset资源文件

    做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...

  3. GO語言基礎教程:數據類型,變量,常量

    GO類似PHP,每行的結尾要加分號來結束,不同點在於GO對此並不強制,這一點又像javascript,另外GO的語句塊是用一對大括號來包裹的,但是go要求左大括號必須要在語句的結尾處,不能在行首出現左 ...

  4. 刷连记录的迟到检测---Table表格增加一列值

    公司OA新增加了 刷脸记录 ,用于查看自己是否迟到,但是没有什么提醒,于是乎自己写了一个脚本 刷连记录 类似于这样的: 运行脚本后,是这个样子的: 擦,我本月已经迟到了 3次了.... 拖拽 刷脸记录 ...

  5. Windows搭建SVN

    1.服务器下载 VisualSVN 地址:http://subversion.apache.org/packages.html 2.然后下载TortoiseSVN客户端,如果要下中文语言包 也在这个页 ...

  6. 原创:goldengate从11.2升级到12.1.2

    goldengate从11.2升级到12.1.2 1.停止抽取进程 GGSCI (001.oracle.drs.dc.com) 286> stop EXTSJ01 2. 停止投递和复制进程 等待 ...

  7. Linux文件和windows文件在 换行符的区别

    Linux或Unix文件,和windows文件,在来回处理时,如果不注意 换行符的区别,可能导致程序错误!!!深刻的教训.... 在早期的打印机时代,开始新的一行要占用两个字符的时间.如果到了一行的结 ...

  8. Navi.Soft30.产品.代码生成器.操作手册

    1系统简介 1.1功能简述 在Net软件开发过程中,大部分时间都是在编写代码,并且都是重复和冗杂的代码.比如:要实现在数据库中10个表的增删改查功能,大部分代码都是相同的,只需修改10%的代码量.此时 ...

  9. Asp.net Core CacheHelper 通用缓存帮助类

    using System; using Microsoft.Extensions.Caching.Memory; using System.Runtime; namespace UFX.Tools { ...

  10. 首次构建android studio gradle 下载缓慢的问题

    1.先使用其他工具下载gradle, https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 2.然后放在C:\Users\A ...