Skip to content

Comment on Classes often aren't the simplest tool for the job

Comments

The JavaScript namespacing example is wonky:

  export default {
    email: "alice@example.com",
    login: function () { ... }
  };
By writing `export`, you’re showing you’re using modules; modules are namespacing, and this file potentially named currentUser.js can contain this instead:
  export let email = "alice@example.com";
  export function login() { ... }
That’s pure namespacing. And yes, export bindings are live, so you can use that like this (though you’re likely to surprise some people—but then, the whole style of the code was dubious in many languages anyway):
  import { email, login } from "./currentUser.js";
  email = "bob@example.com";
  login();
Back to the original example: speaking generally, `export default { … }` is a code smell that should normally be replaced with precise exports, which generally make life easier for tooling and consequently for humans. (As a consumer, the precise exports version requires only changing `import currentUser from "./currentUser.js"` to `import * as currentUser from "./currentUser.js"` in such cases.) Seeing so often such abuse of the default export, I have mused whether perhaps it was a misfeature, an injudicious appeasement and compatibility measure for older patterns.

Default export itself isn’t the misfeature. Mixed named/default exports is. There are several use cases/patterns where a single export is common (though because of the same concerns you raise, and others, I tend to use a single named export anyway).

Ah, you are right. I didn't realize this when I wrote it. Thanks for pointing it out.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.