Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
视图模板——使用
Handlebars
|
65
下面这种做法看起来很“明白”,大概是因为编程课一直都这样教:
10 PRINT "Hello world!"
在命令式语言中,我们都习惯于说“先这样做,再那样做,然后再那样做”。对于某些
事情,这种处理方式还不错。如果需要使用
500
行
JavaScript
代码执行某个复杂的计算
并得出一个结果数值,而中间的每一步都依赖于前一步,那么这种做法没有什么不对。
可是反过来呢?如果你有
500
行
HTML
代码和
3
行
JavaScript
代码
,那么此时写
500
遍
document.write
有什么意义吗?毫无意义。
事实上,以上问题可以归结为:上下文切换带来了问题。如果写了大量的
JavaScript
,却
要混入一些
HTML
,这么做既不方便又令人困惑
。反过来则会好得多。我们都习惯于在
<script>
块里写
JavaScript
,但我希望你能看出差异:上下文切换仍然存在,要么是在写
HTML
,要么是在
<script>
块里写
JavaScript
,让
JavaScript
输出
HTML
问题重重:
•
总是担心什么字符需要转义以及如何转义;
•
使用
JavaScript
生成
HTML
,而
HTML
本身又包含
JavaScript
的话,
很快就会让人抓狂;
•
通常会失去编辑器语法高亮的好处,以及其他的语言特定特性;
•
要发现
HTML
的格式问题变得更困难了;
•
肉眼很难看出代码的结构;
•
其他人理解你的代码会更困难。
模板化解决了这些问题,它允许你使用目标语言来写,同时提供插入动态数据的功能。回
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

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua

Publisher Resources

ISBN: 9787115565099