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

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.

主站蜘蛛池模板: 秦皇岛市| 台山市| 高台县| 三门县| 武邑县| 新乐市| 江口县| 麻江县| 颍上县| 卓尼县| 卢氏县| 海淀区| 宁阳县| 孝义市| 高安市| 客服| 襄城县| 和平县| 历史| 平邑县| 商水县| 平安县| 昔阳县| 浪卡子县| 西乌珠穆沁旗| 元氏县| 福州市| 荆州市| 南雄市| 龙川县| 广饶县| 中牟县| 湘阴县| 白玉县| 纳雍县| 酒泉市| 普兰县| 宁强县| 罗城| 广安市| 安丘市|