Chapter 4: Node JavaScript
Writing JavaScript for Node.JS and the browser is a remarkably different experience. Node.JS takes the basic language, and just like browsers did, adds different APIs on top of it to ensure writing code that’s meant to power networked applications feels as natural as possible.
Throughout this chapter you will examine certain APIs that are not part of the language as it was conceived in its specification, but that both Node and browsers have. But more importantly, you will also go through the core Node.JS additions that are considered, as the title of this chapter implies, “Node JavaScript.”
The first difference you’ll look at pertains to the global object.
The global object
In the browser, window
is the global object. Anything that you define in window
becomes available to all parts of your code. For example, setTimeout
is in reality window.setTimeout
, and document
is window.document
.
Node has two similar objects that provide a cleaner separation:
• global
: Just like window
, any property attached to global
becomes a variable you can access anywhere.
• process
: Everything that pertains to the global context of execution is in the process
object. In the browser, there’s only one window, and in Node, there’s only one process at any given time. As an example, in the browser, the window name is window.name
, and in Node, the name of the process is process.title
.
Later chapters dig deeper into the process
object because it provides broad and interesting functionality, ...
Get Smashing Node.js: JavaScript Everywhere, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.