官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 比如县| 永清县| 宣恩县| 波密县| 江城| 木兰县| 石狮市| 平江县| 乌拉特中旗| 泽州县| 礼泉县| 威远县| 哈尔滨市| 新和县| 沾益县| 孙吴县| 新野县| 双柏县| 霍城县| 长顺县| 承德市| 象山县| 繁昌县| 河源市| 萨嘎县| 白沙| 肃北| 图片| 治多县| 巴里| 金湖县| 分宜县| 静乐县| 土默特右旗| 云南省| 闵行区| 通榆县| 子长县| 济宁市| 徐水县| 眉山市|