- Mastering Node.js(Second Edition)
- Sandro Pasquali Kevin Faaborg
- 365字
- 2021-07-02 19:28:38
Extending JavaScript
When he designed Node, JavaScript was not Ryan Dahl's original language choice. Yet, upon exploration, he found a good modern language without opinions on streams, the filesystem, handling binary objects, processes, networking, and other capabilities one would expect to exist in a systems language. JavaScript, strictly limited to the browser, had no use for, and had not implemented, these features.
Guided by the Unix philosophy, Dahl was guided by a few rigid principles:
- A Node program/process runs on a single thread, ordering execution through an event loop
- Web applications are I/O intensive, so the focus should be on making I/O fast
- Program flow is always directed through asynchronous callbacks
- Expensive CPU operations should be split off into separate parallel processes, emitting events as results arrive
- Complex programs should be assembled from simpler programs
The general principle is, operations must never block. Node's desire for speed (high concurrency) and efficiency (minimal resource usage) demands the reduction of waste. A waiting process is a wasteful process, especially when waiting for I/O.
JavaScript's asynchronous, event-driven design fits neatly into this model. Applications express interest in some future event, and are notified when that event occurs. This common JavaScript pattern should be familiar to you:
Window.onload = function() {
// When all requested document resources are loaded,
// do something with the resulting environment
}
element.onclick = function() {
// Do something when the user clicks on this element
}
The time it will take for an I/O action to complete is unknown, so the pattern is to ask for notification when an I/O event is emitted, whenever that may be, allowing other operations to be completed in the meantime.
Node adds an enormous amount of new functionality to JavaScript. Primarily, the additions provide evented I/O libraries offering the developer system access not available to browser-based JavaScript, such as writing to the filesystem or opening another system process. Additionally, the environment is designed to be modular, allowing complex programs to be assembled out of smaller and simpler components.
Let's look at how Node imported JavaScript's event model, extended it, and used it in the creation of interfaces to powerful system commands.
- 網絡協議工程
- 光網絡評估及案例分析
- Hands-On Industrial Internet of Things
- 面向云平臺的物聯網多源異構信息融合方法
- 物聯網工程導論(第3版)
- 網絡綜合布線(第2版)
- 轉化:提升網站流量和轉化率的技巧
- 組網技術與網絡管理
- 計算機網絡技術
- Getting Started with tmux
- 物聯網M2M開發技術:基于無線CPU-Q26XX
- 物聯網,So Easy!
- SD-WAN 架構與技術
- Microsoft System Center 2012 Configuration Manager:Administration Cookbook
- Full-Stack Web Development with Vue.js and Node