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
6.5
 无状态
149
6.5
 无状态
我们已经探讨过,未加控制的状态会如何直接导致应用程序崩溃,而尽可
能减少状态的数量可使应用程序更易于调试。全局状态越少,应用程序在
当前状态下任一时刻所出现的不可预测性就越小,并且在调试时遇到的意
外就越少。
缓存是一种特别隐蔽的状态形式。它在大多数情况下能避免高成本的查找
操作,因而可以有效提高应用程序的性能。当使用状态管理工具作为缓存
机制时,我们可能陷入陷阱 :派生的应用程序的不同状态在不同的时间点
被派生出来,因此会使用不同时间点计算出的数据来渲染应用程序的不同
部分。
在大多数情况下,不应该将派生状态视为与其原始数据互不关联的一种状
态。不然,我们就可能遇到这样的情况 :原始数据被更新了,但派生状态
却没有,因此派生状态就是过时和不准确的。而如果我们总是根据原始数
据计算派生状态,则会降低这种派生状态过时的可能性。
状态几乎无处不在,实际上可以将其视为应用程序的同义词,因为没有状
态的应用程序基本上没什么用。那么问题出现了 :如何才能更好地管理状
态?以典型的
Web
服务器等应用程序为例,它们的主要工作是接收请求、
处理请求并发回适当的响应。因此,
Web
服务器将状态与每个请求相关联,
将其放在请求处理程序(与请求状态最相关的使用者)附近。当涉及
Web
服务器时,每个请求
/
响应周期中包含了绝大多数的状态,使全局状态尽
可能地少。通过这种方式,
Web
服务器在设置具有多个服务器节点的水平
扩展时,为自己省去了很多麻烦。这样,
Web
服务器就不需要为了在服务
150
6
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