Choosing What a Module Shares

Your digest.js can now import its first function from "./readings.js". Porting the rest of that file raises a question Dev never had to answer in Python: Which of its functions may Asha use?

In Python, the answer was all of them, without anyone deciding. In JavaScript, the module decides, and every file’s imports name exactly what it uses and where it comes from.

Mark What Leaves the File

Python shares every top-level name. Dev’s digest.py imports all three helpers, and nothing in readings.py has to allow it:

Language: Python
from readings import is_blank, parse_line, summarise

Even a name with a leading underscore, such as _is_blank, imports without complaint when you name it. The underscore only keeps it out of from readings import *, as __all__ does.

Both languages give each module its own scope, but a Python import reads any top-level name off the module, while a JavaScript import sees only the names marked with export. Here’s the port, with the loop in summarise() left out:

Language: JavaScript Filename: readings.js
export function isBlank(line) {
  return line.trim() === "";
}

export function parseLine(line) {
  const [time, temp] = line.trim().split(",");
  return { time, temp: Number(temp) };
}

export function summarise(readings, threshold) {
  // The loop that finds the peak and the readings over the limit
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.