Posts

ES6 classes have inner names

This blog post explains that classes have lexical inner names, just like named function expressions. The inner names of function expressions You may know that function expressions have lexical inner names: const fac = function me(n) { if (n > 0) { // Use inner name `me` to // refer to the function return n * me(n-1); } else { return 1; } }; console.log(fac(3)); // 6 The name me of the named function expression becomes a lexically bound variable that is unaffected by which variable currently holds the function. The inner names of classes Interestingly, ES6 classes also have lexical inner names that you can use in methods (constructor methods and regular methods): class C { constructor() { // Use inner name C to refer to class console.log(`constructor: ${C.prop}`); } logProp() { // Use inner name C to refer to class console.lo...

Running locally installed npm executables

One nice npm feature is that you can install packages with executables locally. This blog post explains how to run locally installed executables. Running executables from a nearby node_modules (An aside, on the topic of packages versus modules: npm packages may or may not contain Node.js modules.) If you require a module, Node.js looks for it by going through all node_modules/ directories in ancestor directories ( ./node_modules/ , ../node_modules/ , ../../node_modules/ , etc.). The first appropriate module that is found is used. Whenever you are somewhere in the file system, npm root tells you where it would install packages if you used npm install . That directory node_modules/ may or may not exist, already; in the following example, directory /tmp/ is empty. $ cd /tmp/ $ npm root /tmp/node_modules When executables are installed via npm packages, npm links to them: In local installs, they are linked to from a node_modules/.bin/ directory. In global installs, they are...

Revenge and War

Image
Stories of adventure and survival against the odds abound in literature.   I stayed up many a night reading into the small hours of the morning because I could not put such novels down.   I had to race through to the end to see what happens.   The best ones kept me reading because the dangers and threats encountered by the protagonist seemed inescapable, and the good writers made the characters’ survival both realistic yet unpredictable; that is not an easy task.   And if they died, the death was of a scope worthy of such heroes.   Such is the power of the adventure narrative. Two novels that have beguiled me lately are The Revenant:   A Novel of Revenge (Picador, 2015) by Michael Punke, and Three Day Road (Penguin, 2005) by Joseph Boyden.   It should be noted that I have not seen the Leonardo DiCaprio film of The Revenant , so I will be focusing only on the novel. Michael Punke , himself, has an interesting back story.   He is the Deputy Unite...

Managing the private data of ES6 classes

This blog post explains four approaches for managing private data for ES6 classes: Keeping private data in the environment of a class constructor Marking private properties via a naming convention (e.g. a prefixed underscore) Keeping private data in WeakMaps Using symbols as keys for private properties Approaches #1 and #2 were already common in ES5, for constructors. Approaches #3 and #4 are new in ES6. Let’s implement the same example four times, via each of the approaches. Keeping private data in the environment of a class constructor Our running example is a class Countdown that invokes a callback action once a counter (whose initial value is counter ) reaches zero. The two parameters action and counter should be stored as private data. In the first implementation, we store action and counter in the environment of the class constructor. An environment is the internal data structure, in which a JavaScript engine stores the parameters and local variables that come into existen...

Mother

Image
Ten years ago today, my mother dropped dead on the driveway of a friend’s house after a New Year’s party.   She was 62.   She was not well and hadn’t been for years. At the hospital as we stood over her lifeless body, a nurse pulled me aside and asked me to witness her removing my mother’s jewelry and personal effects so they could put her in the body bag.   We walked around the bed removing bracelets and rings, a small pendant, and finally, her worn shoes.   Everything went into a large zip lock bag with her name on it:   Patricia Martin.   The nurse handed me the bag and told me she was sorry for my loss. In the end, we leave this world as we entered it:   naked and alone. After her death, I dreamed of her a few times until she told me that she would no longer be able to visit me.   I understood this to mean that she had to move on to whatever existence was ahead of her and leave this life behind. So today, I bear witness to her life, to wh...

Looking back on 2015: six exciting web technologies

In 2015, there was an amazing amount of innovation related to the web platform. The following sections describe six technologies that I find exciting: Electron React Native Progressive web apps Visual studio code Rollup Web Assembly This blog post is a loose follow-up to “ Web platform: five technologies to look forward to in 2014 ”, which I wrote in early 2014. Electron Electron (by GitHub) lets you build cross-platform desktop apps with web technologies. Its features include: Automatic updates Crash reporting Windows installers Debugging and profiling Native menus and notifications Electron was initially created for GitHub’s editor Atom and is now used by various companies, including Microsoft (Visual Studio Code, see below), Slack and Docker. Architecturally, Electron contains both a Node.js runtime and a minimal embedded Chromium browser. Electron apps run in several processes: A main process runs the main script specified by the app’s package.json file. To display a user inte...

My new book: “Setting up ES6”

Image
My latest book is called “ Setting up ES6 ”. It covers the following topics: A cheat sheet for deploying ECMAScript 6 Example setups (skeleton projects that you can download from GitHub): ES6 in browsers via webpack and Babel ES6 in Node.js via Babel (compiled dynamically or statically) How to configure Babel 6, including a clear explanation of how it interacts with CommonJS modules “ Setting up ES6 ” was conceived as a companion to my other book, “ Exploring ES6 ”: “ Exploring ES6 ” is supposed to remain relevant for a longer time, so that a print edition makes sense (which I still intend to publish, hopefully by mid-2016). “ Setting up ES6 ” will age more quickly. It mainly documents my attempts to understand how Babel 6 works. In a way, you could call it “Setting up Babel 6”. But it has a slightly broader scope, especially the first chapter on deploying ES6. Happy reading – the contents of “ Setting up ES6 ” are free to read online. If you like the book then you can support my work ...