Skip to Content
精通模块化JavaScript
book

精通模块化JavaScript

by Nicolás Bevacqua
April 2020
Intermediate to advanced
177 pages
1h 45m
Chinese
Publishing House of Electronics Industry
Content preview from 精通模块化JavaScript
4.2
 重构复杂代码
81
守卫语句的另一个好处几乎难以察觉 :因为它们被放到函数的顶部附近,
所以我们可以快速访问其参数,更好地理解函数如何校验其输入,然后我
们就可以更高效地决定是否需要添加新的守卫语句来改进校验规则。
虽然守卫语句不会告诉读者在调用一个函数时他们需要了解的所有可能出
错的情况,但可以让他们窥见那些预期的立即失败的情况。其他可能导致
错误的情况藏在该函数的具体实现中。也许你使用不同的服务或者库完成
函数的大部分任务,这些服务或者库有自己的一组嵌套守卫语句和可能的
失败情况,它们会冒泡到你自己的函数结果中。
4.2.3
 依赖金字塔
写直白的代码跟写直白的文章相比没多少不同。文章通常被组织成段落的
形式,可以将段落看成代码中的函数。我们可以认为这个函数的输入就是
读者的知识和他们从文章中读到的所有东西,而输出则是读者从该段落中
得到的知识。
在一本书的章节或者篇幅较长的一篇文章中,段落的组织都是循序渐进的,
好让读者一段一段地消化。其逻辑顺序是精心安排好的 :如果没有连贯的
顺序,几乎没办法读懂文章。因此,作者会力保讨论某个概念之前先介绍
这个概念,以便给读者提供上下文。
例如下面的代码片段,在包含赋值的那一行代码被执行前,函数表达式不
会赋值给对应变量。只有代码被执行后,对应的变量绑定在作用域中才存在,
因为变量声明的位置被提升了,在赋值语句被执行前它都是
undened
double(6) //
类型错误
: double
不是一个函数
var double = function(x) {
82 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
基于Python的智能文本分析

基于Python的智能文本分析

Benjamin Bengfort, Rebecca Bilbro, Tony Ojeda
Lua游戏AI开发指南

Lua游戏AI开发指南

Posts & Telecom Press, David Young

Publisher Resources

ISBN: 9787121387074