[20141213]编写高质量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.26、使用bind方法实现函数柯里化

Tips:

  1. 使用bind方法实现函数柯里化,即创建一个固定需求参数子集的委托函数
  2. 传入null或undefined作为接收者的参数来实现函数柯里化,从而忽略其接收者

什么是函数柯里化?

将函数与其参数的一个子集绑定的技术称为函数柯里化,它是一种简洁的、使用更少引用来实现函数委托的方式。

//有一个组装URL的JS函数
function bulidURL(protocol, domain, path){
return protocol + '://' + domain + '/' + path;
} //需要一个path数组转换为url数组,那么一般做法是:
var urls = paths.map(function(path){
return bulidURL('http', 'www.hstar.org', path);
}); 如果用bind实现函数柯里化,则是:
var buildURL2 = buildURL.bind(null, 'http', 'www.hstar.org');
var urls = paths.map(buildURL2); 其中由于buildURL不引用this,那么在bind中使用null,忽略函数本身的接收者,然后用bind实现柯里化。
使用buildURL.bind的参数+buildURL2的参数结合起来调用buildURL方法。
可以在bulidURL中写console(arguments)来查看参数合集。

No.27、使用闭包而不是字符串来封装代码

Tips:

  1. 当将字符串传递给eval函数以执行它们的API时,绝不要在字符串中包含局部变量引用
  2. 接受函数调用的API优于使用eval函数执行字符串的API

JS中,函数是一个将代码作为数据结构存储的便利方式,这些代码可以后面被执行。所以可以在JS中编写富有表现力的高阶函数,如map,forEach。

比较不好的设计,使用eval函数执行字符串。

//定义一个函数,使用eval执行字符串
function fun1(code){
eval(code);
} //用法一:
var val = 0;
fun1('console.log(val)'); //用法二:
function fun2(){
var val = 1;
fun1('console.log(val)');
}
fun2(); //Error:val is not defined

警告:在使用eval的时候,作用域是全局作用域(window),如用法一的调用,刚好能够出正常结果;如果转移到函数体内,如用法二的调用,则会出现错误;最坏的情况是用法二调用时,全局作用域上刚好有个同名的变量(本例中为val),那么将会让结果无法预期。

好的做法,就是直接传递函数

function fun1(){

}
function fun2(p, action){
if(p === 1){
action();
}
} fun2();

No.28、不要依赖函数对象的toString方法

Tips:

  1. 调用函数的toString方法时,并没有要求JavaScript引擎能够精确的获取到函数的源代码
  2. 由于在不同的引擎下调用toString方法的结果可能不同,所以绝不要信赖函数源代码的详细细节
  3. toString方法的执行结果并不会暴露存储在闭包中的局部变量值
  4. 通常情况下,应该避免使用函数对象的toString方法

JavaScript函数有一个非凡的特性,即将其源代码重现为字符串的能力。但是ECMAScript标准对toString返回的字符串没有任何要求,所以不同引擎产生的结果可能不同。甚至返回到字符串和该函数并不相关

No.29、避免使用非标准的栈检查属性

Tips:

  1. 避免使用非标准的arguments.caller和arguments.callee属性,因为它们不具备良好的移植性
  2. 避免使用非标准的函数对象caller属性,因为在包含全部栈信息方面,它是不可靠的

基本错误(不推荐使用)

function getCallStack(){
var stack = [];
for(var f = getCallStack.caller; f; f = f.caller){
stack.push(f);
}
return stack;
}

警告:该函数非常脆弱,如果某函数叜调用栈中出现了不止一次,那么栈检查会陷入死循环。同时使用caller在ES5的严格模式下会error。

编写高质量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个有效方法(四)

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

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

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

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

    [20141011]编写高质量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. Android图片处理-相机、相处简单调用

    安卓开发中,常常需要使用到手机相机拍照.或者相册上传头像等等.通过使用Intent,我们很方便地获得相机.相册里面的图片: 1.相机调用,通过设置File文件路径和文件名,可以将拍照得到的图片保存下来 ...

  2. The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread

    01-Jul-2016 14:25:30.937 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoade ...

  3. Android SDK下载和更新失败的解决方法【转】

    启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口:在『Androi ...

  4. 连接UI到代码

    本章,你将连接FoodTracker应用程序的UI到代码并定义一些可执行的动作.当你完成时,你的应用程序将是这个样子: 学习目标在课程结束时,你将能够:1.解释一个storyboard中的场景和vie ...

  5. [Aaronyang] 写给自己的WPF4.5 笔记20 [3d课 1/4]

    假设你是高中毕业的,ok,数学知识几何知识中,我们学过  x,y,z   3个轴然后就可以画出形状了. 1. 新建空白窗体,grid换成canvas,然后新增一个Viewport3D元素 3d中显示的 ...

  6. java.lang.OutOfMemoryError: PermGen space PermGen space & java.lang.OutOfMemoryError: Java heap space Heap siz

    java.lang.OutOfMemoryError: PermGen space PermGen space 由-XX:PermSize  -XX:MaxPermSize 引起 java.lang. ...

  7. 九宫格抽奖HTML+JS版

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. BW CUBE 数据的聚集和压缩

    大家都知道,压缩和聚集都是提高bw性能的方法,在新版bw里,压缩名称改成了折叠,聚集放在了滚动菜单里---集合.那么我们在使用这两种方法时需要注意的先创建聚集以后,再进行压缩,因为压缩的过程是把F表的 ...

  9. 【面经】Epic: 数据库去重

    题目是:有2个10G的数据库,存储了一些string. 2者之间有一些重复的数据.请把它们合并为一个数据库,并且去除重复. 限制:内存是4G 例如: DB1: cmu, ucb, stanford, ...

  10. (转)数字格式化函数:Highcharts.numberFormat()

    一.函数说明 该函数用于图表中数值的格式化,常见用途有数值精度控制.小数点符.千位符显示控制等.   二.函数使用   1.函数构造及参数 Highcharts.numberFormat (Numbe ...