[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. javaweb回顾第五篇浅谈会话

    1:什么是会话 通俗来说就是客户和服务器的一次私密谈话,客户发送请求以后服务器能够识别请求是来自同一个客户,他们是1对1的关系. 了解会话以后我们就要去考虑如何去实现这些问题下面一一进行解析 2:会话 ...

  2. ios之VFL的补充(二)

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[languageI ...

  3. 【转】C++11 标准新特性: 右值引用与转移语义

    VS2013出来了,对于C++来说,最大的改变莫过于对于C++11新特性的支持,在网上搜了一下C++11的介绍,发现这篇文章非常不错,分享给大家同时自己作为存档. 原文地址:http://www.ib ...

  4. Android高仿微信图片选择功能的PhotoPicker

    类似于微信修改头像的功能基本上每个app都会有,以前公司开发的项目就有修改头像的功能,但是用的Android系统自带的图片选择器.用Android系统的图片选择器有个好处就是稳定,不会有什么问题.但也 ...

  5. 【Android开发坑系列】之事件

    总结一下: 1.Touch事件分发中只有两个主角:ViewGroup和View.ViewGroup包含onInterceptTouchEvent.dispatchTouchEvent.onTouchE ...

  6. 表单验证Jquery扩展方法类

    /** 表单数据验证 **/ $.fn.Validform = function () { var Validatemsg = ""; var Validateflag = tru ...

  7. [转]gulp构建前端工程

    摘要: Gulp 是一个自动化工具,前端开发者可以使用它来处理常见任务: 搭建web服务器 文件保存时自动重载浏览器 使用预处理器如Sass.LESS 优化资源,比如压缩CSS.JavaScript. ...

  8. Win7 安装Apache 2.2.4报错:<OS 5>拒绝访问. :Failed to open the WinNT service manager

    Apache 2.2安装档与win7的“用户账户控制”冲突,所以只要先关闭,安装完后再开启即可. 重启,然后再重新安装Apache2.2.4,没有出现错误.然后再把上述设置改回去. 参考:http:/ ...

  9. ASP.NET 4.0 forms authentication issues with IE11

    As I mentioned earlier, solutions that rely on User-Agent sniffing may break, when a new browser or ...

  10. No Assistant Results

    由于修改一些文件名字等会导致这个不工作. "Organizer" / "Projects" / 选择你的项目.  "Delete" .