编写高质量JS代码的68个有效方法(十二)
No.56、避免不必要的状态
Tips:
- 尽可能地使用无状态的API
- 如果API是有状态的,标示出每个操作与哪些状态有关联
无状态的API简洁,更容易学习和使用,也不需要考虑其他的状态。如:
'test'.toUpperCase(); // 'TEST'
有状态的API往往会导致额外的声明,并增加复杂度。
No.57、使用结构类型设计灵活的接口
Tips:
- 使用结构类型(也称为鸭子类型)来设计灵活的对象接口
- 结构接口更灵活、更轻量,所以应该避免使用继承
- 针对单元测试,使用mock对象即接口的替代实现来提供可复验的行为
直接上代码:
function Wiki(format){
this.format = format;
}
Wiki.prototype.show = function(source){
var page = this.format(source);
return {
title: page.getTitle(),
author: page.getAuthor(),
content: page.getContent()
}
}
将format设计为结构类型,可以极大的增加设计的灵活性。
No.58、区分数组对象和类数组对象
Tips:
- 绝不重载与其他类型有重叠的结构类型
- 当重载一个结构类型与其他类型时,先测试其他类型
- 当重载其他对象类型时,接收真数组而不是类数组对象
API绝不应该重载与其他类型有重叠的类型
最简单的判断数组与类数组,代码如下:
x instanceof Array
但是,在一些允许多个全局对象的环境中可能会有标准的Array构造函数和原型对象的多份副本。那么就有可能导致以上的测试结果不可信,所以在ES5引入了Array.isArray函数来判断是否是Array对象,通过检查对象内部[[Class]]属性值是否为Array来判定。在不支持ES5的环境中,可以使用标准的Object.prototype.toString方法测试一个对象是否为数组。
function isArray(x){
return toString.call(x) === '[object Array]';
}
No.59、避免过度的强制转换
Tips:
- 避免强制转换和重载的混用
- 考虑防御性地监视非预期的输入
看以下的函数:
function square(x){
return x*x;
}
console.log(square('3')); // 9
强制转换无疑是很方便的。但很多时候却会导致含糊不清。
function fun(x){
x = Number(x);
if(typeof x === 'number'){
return x-1;
}else{
return x;
}
}
由于进行了Number(x),那么后面的else是无法执行到的。如果不知道这个函数的细节,那么使用该函数则具有一定的模糊性。 事实上,如果我们要更小心的设计API,我们可以强制只接受数字和对象。
function fun(x){
if(typeof x === 'number'){
return x-1;
}else if(typeof x === 'object' && x){
return x;
}else{
throw new TypeError('expected number or array-like.');
}
}
这种风格更加谨慎的示例,被称为防御性编程。
No.60、支持方法链
Tips:
- 使用方法链来连接无状态的操作
- 通过在无状态的方法中返回新对象来支持方法链
- 通过在有状态的方法中返回this来支持方法链
无状态的API部分能力是讲复杂操作分解为更小的操作。如replace:
function escapeHtml(str){
return str.replace(/&/g, '&')
.replace(/</g, '<');
}
如果不采用方法链方式,代码应该是以下这样:
function escapeHtml(str){
var str1 = str.replace(/&/g, '&');
var str2 = str1.replace(/</g, '<');
return str2;
}
同样的功能,将会产生多个临时变量。消除临时变量使得代码更加可读,中间结果只是得到最终结果中的一个重要步骤而已。
在有状态的API中设置方法链也是可行的。技巧是方法在更新对象时返回this,而不是undefined。如:
element.setBackgroundColor('gray')
.setColor('red')
.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个有效方法(十二)的更多相关文章
- 编写高质量JS代码的68个有效方法(二)
[20141011]编写高质量JS代码的68个有效方法(二) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(八)
[20141227]编写高质量JS代码的68个有效方法(八) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(七)
[20141220]编写高质量JS代码的68个有效方法(七) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(六)
[20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(四)
[20141129]编写高质量JS代码的68个有效方法(四) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- 编写高质量JS代码的68个有效方法(三)
[20141030]编写高质量JS代码的68个有效方法(三) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...
- JavaScript手札:《编写高质量JS代码的68个有效方法》(一)(1~5)
编写高质量JS代码的68个有效方法(一) *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...
- 编写高质量JS代码的68个有效方法(十三)
No.61.不要阻塞I/O事件队列 Tips: 异步API使用回调函数来延缓处理代价高昂的操作以避免阻塞主应用程序 JavaScript并发的接收事件,但会使用一个事件队列按序地处理事件处理程序 在应 ...
- 编写高质量JS代码的68个有效方法(十)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 编写高质量JS代码的68个有效方法(十一)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
随机推荐
- CSS3参考手册
很好用的CSS3 API http://www.css88.com/book/css/
- SAFS Init Files
There're many deployment files for configuration. We need to learn how SAFS read these depolyment fi ...
- Help Viewer 2.2 独立运行
"C:\Program Files (x86)\Microsoft Help Viewer\v2.2\HlpViewer.exe" /catalogName VisualStudi ...
- 在VC项目中附加包含目录
1.VC2010项目中附加包含目录 上图项目中附加了两个文件夹,一个是上级目录下的CommonClass,一个是下级目录下的invengo. 使用这两个目录下的类时直接在include后面写头文件名即 ...
- spring官方案例程序
https://github.com/spring-projects/spring-data-book https://github.com/spring-projects 包含其他相关的应用程序
- WIN7只能上QQ打不开网页,使用CMD输入netsh winsock reset
此类问题可以用腾讯电脑管家电脑诊所一键修复,请点击上方的[立即修复]即可. 附:手动修复步骤(来源:腾讯电脑管家电脑诊所,自动修复请点击上方的[立即修复])方案一:手动设置DNS(说明:如果您使用DN ...
- C++ Data Member内存布局
如果一个类只定义了类名,没定义任何方法和字段,如class A{};那么class A的每个实例占用1个字节的内存,编译器会会在这个其实例中安插一个char,以保证每个A实例在内存中有唯一的地址,如A ...
- elasticsearch-cn-out-of-box
elasticsearch-cn-out-of-box https://github.com/hangxin1940/elasticsearch-cn-out-of-box 为elasticsearc ...
- 无须任何软件配置iis+ftp服务器图文说明
1.1 检查是否安装已安装IIS6组件 在windows service 2003 操作系统中,windows组件“IIS6.0”是用户搭建站点以及ftp文件共享的服务器. 具体检查步骤如下: 进入“ ...
- 如何升级TeX Live 2014宏包
转:人大经济论坛 LATEX论坛 版,详细出处参考: http://bbs.pinggu.org/forum.php?mod=viewthread&tid=3370640&page=1 ...