[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. textViewDidChange: crashes in iOS 7

    What's happening is that you're typing what is referred to as multistage text input, i.e. the input ...

  2. Windows出现BOOT\BCD错误的解决办法

    这篇文章主要介绍了Windows出现BOOT\BCD错误的解决办法,本文讲解使用命令的方式解决这个问题,需要的朋友可以参考下 一般碰到 Boot Record Error 问题用系统盘自动修复一下就可 ...

  3. 旧手机作为USB无线网卡使用(分享WIFI、蓝牙连接)

    首先开启手机的WIFI或者蓝牙功能,建立访问互联网的连接,然后设置-更多-网络共享与便携热点,打开安卓手机USB网络共享功能,即可在计算机上通过手机(无电话卡.数据卡)访问互联网.而且此时手机一直在充 ...

  4. pgpgin|pgpgout|pswpin|pswpout意义与差异

    引用来自: http://ssms.cs2c.com.cn/otrs/pc.pl?Action=PublicFAQZoom;ItemID=11741 文章主要意思是: 1. page in/out操作 ...

  5. PPTP VPN 一键安装包(图文,OpenVZ适用)[zz]

    [介绍] VPN的英文全称是“Virtual Private Network”,中文名叫“虚拟专用网络”.VPN可以通过特殊加密的通讯协议连接到Internet上,在位于不同地方的两个或多个内部网之间 ...

  6. Web 开发常备工具

    工欲善其事,必先利其器.如今 Web 开发标准越来越高,Web 开发者也在不断寻找途径提升自己的技能.为使大家的开发工作更顺利进行,本文整理了 10+ 款比较优秀的 Web 开发工具,希望对你有帮助. ...

  7. 你可能不知道的python

    1.如何循环获得下标,使用 enumerate ints = ['a','b','c','d','e','f'] for idx, val in enumerate(ints): print idx, ...

  8. 用issnode+IIS来托管NodeJs Server

    用issnode+IIS来托管NodeJs Server之一:安装篇 用issnode+IIS来托管NodeJs Server之二:移植 用issnode+IIS来托管NodeJs Server之三: ...

  9. 解决微信浏览器无法使用reload()刷新页面

    场景是这样子的,页面在初始化时有一个ajax请求,在页面上有一个按钮,点击的时候执行window.location.reload(),正常情况reload()后页面依然会向后台发出请求,但在安卓的微信 ...

  10. Eclipse远程调试HDP源代码

    使用的是自己编译的HDP2.3.0的源代码编译的集群,此文介绍如何使用Eclipse远程调试Hadoop内核源代码,以调试namenode为例进行介绍. 在/usr/hdp/2.3.0.0-2557/ ...