No.56、避免不必要的状态

Tips:

  1. 尽可能地使用无状态的API
  2. 如果API是有状态的,标示出每个操作与哪些状态有关联

无状态的API简洁,更容易学习和使用,也不需要考虑其他的状态。如:

  1. 'test'.toUpperCase(); // 'TEST'

有状态的API往往会导致额外的声明,并增加复杂度。

No.57、使用结构类型设计灵活的接口

Tips:

  1. 使用结构类型(也称为鸭子类型)来设计灵活的对象接口
  2. 结构接口更灵活、更轻量,所以应该避免使用继承
  3. 针对单元测试,使用mock对象即接口的替代实现来提供可复验的行为

直接上代码:

  1. function Wiki(format){
  2. this.format = format;
  3. }
  4. Wiki.prototype.show = function(source){
  5. var page = this.format(source);
  6. return {
  7. title: page.getTitle(),
  8. author: page.getAuthor(),
  9. content: page.getContent()
  10. }
  11. }

将format设计为结构类型,可以极大的增加设计的灵活性。

No.58、区分数组对象和类数组对象

Tips:

  1. 绝不重载与其他类型有重叠的结构类型
  2. 当重载一个结构类型与其他类型时,先测试其他类型
  3. 当重载其他对象类型时,接收真数组而不是类数组对象

API绝不应该重载与其他类型有重叠的类型

最简单的判断数组与类数组,代码如下:

  1. x instanceof Array

但是,在一些允许多个全局对象的环境中可能会有标准的Array构造函数和原型对象的多份副本。那么就有可能导致以上的测试结果不可信,所以在ES5引入了Array.isArray函数来判断是否是Array对象,通过检查对象内部[[Class]]属性值是否为Array来判定。在不支持ES5的环境中,可以使用标准的Object.prototype.toString方法测试一个对象是否为数组。

  1. function isArray(x){
  2. return toString.call(x) === '[object Array]';
  3. }

No.59、避免过度的强制转换

Tips:

  1. 避免强制转换和重载的混用
  2. 考虑防御性地监视非预期的输入

看以下的函数:

  1. function square(x){
  2. return x*x;
  3. }
  4. console.log(square('3')); // 9

强制转换无疑是很方便的。但很多时候却会导致含糊不清。

  1. function fun(x){
  2. x = Number(x);
  3. if(typeof x === 'number'){
  4. return x-1;
  5. }else{
  6. return x;
  7. }
  8. }

由于进行了Number(x),那么后面的else是无法执行到的。如果不知道这个函数的细节,那么使用该函数则具有一定的模糊性。 事实上,如果我们要更小心的设计API,我们可以强制只接受数字和对象。

  1. function fun(x){
  2. if(typeof x === 'number'){
  3. return x-1;
  4. }else if(typeof x === 'object' && x){
  5. return x;
  6. }else{
  7. throw new TypeError('expected number or array-like.');
  8. }
  9. }

这种风格更加谨慎的示例,被称为防御性编程。

No.60、支持方法链

Tips:

  1. 使用方法链来连接无状态的操作
  2. 通过在无状态的方法中返回新对象来支持方法链
  3. 通过在有状态的方法中返回this来支持方法链

无状态的API部分能力是讲复杂操作分解为更小的操作。如replace:

  1. function escapeHtml(str){
  2. return str.replace(/&/g, '&')
  3. .replace(/</g, '&lt;');
  4. }

如果不采用方法链方式,代码应该是以下这样:

  1. function escapeHtml(str){
  2. var str1 = str.replace(/&/g, '&amp;');
  3. var str2 = str1.replace(/</g, '&lt;');
  4. return str2;
  5. }

同样的功能,将会产生多个临时变量。消除临时变量使得代码更加可读,中间结果只是得到最终结果中的一个重要步骤而已。

在有状态的API中设置方法链也是可行的。技巧是方法在更新对象时返回this,而不是undefined。如:

  1. element.setBackgroundColor('gray')
  2. .setColor('red')
  3. .setFontweight('bold');

*: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%
}
-->

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Struts2学习笔记-jsp中引用struts2框架

    如果在jsp中需要引用struts2 框架,需在前面加上以下内容 <%@taglib prefix="s" uri="/struts-tags" %> ...

  2. Java null String

    In Java, the String will have different usage. Example: public class Test { public static void main( ...

  3. ArcGIS Flex API加载大量数据

    1.关于大量数据的加载: 直接将所要展示的数据在服务器端发布成一个MapService,在客户端通过ArcGISDynamicMapServiceLayer来加载.这样的话客户端需要展示的仅仅是一张图 ...

  4. android: permission和uses-permission

    首先,先看一下permission定义的格式: <permission android:description="string resource" android:icon= ...

  5. windows批处理

    1.日期作为变量当做文件名的一部分. C:\Documents and Settings\Simon>echo %date%2008-09-09 星期二 C:\Documents and Set ...

  6. 《STL系列》之map原理及实现

    上一篇文章<STL系列>之vector原理及实现,介绍了vector的原理及实现,这篇文章介绍map的原理及实现.STL实现源码下载.STL中map的实现是基于RBTree的,我在实现的时 ...

  7. 让Asp.Net WebAPI支持OData查询,排序,过滤。

    让Asp.Net WebAPI支持OData后,就能支持在url中直接输入排序,过滤条件了. 一.创建Asp.Net WebAPI项目: 二.使用NuGet安装Asp.Net WebAPI 2.2和O ...

  8. DataTable 中varchar 转换为 Double 后重新 排序。

    DataTable  查询出某个字段为varchar 类型的.不过里面存的为数字,需要进行排序.可是如果直接排序就会不对.因为为varchar类型的,需要转换一下. 方法一: dt.Columns.A ...

  9. go2shell的安装与修改默认terminal方法

    go2shell的安装与修改默认terminal方法   1. 安装go2shell后,打开finder的application文件夹,找到go2shell 2. 按住command,用鼠标将go2s ...

  10. ABAP程序中关于长文本的处理方法

    现象描述 长文本在SAP的运用主要体现在一些notes的记录,或者一些比较长的文本的存取,比如工作流的审批意见,采购申请和采购订单的附加说明等等.如下图: 处理过程 1:SAP中所有的长文本都存在两张 ...