Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A namespace import is an ECMAScript module (ESM) syntax that gathers all exported bindings from a specified module and attaches them as properties to a single, locally scoped object. This mechanism creates a Module Namespace Exotic Object, allowing access to a module’s entire exported API surface through a single identifier.

Syntax

import * as namespaceIdentifier from "module-specifier";
  • *: Represents all named and default exports from the target module.
  • as namespaceIdentifier: Binds the imported Module Namespace Object to a local variable name.
  • module-specifier: The string literal path to the module.

The Module Namespace Object

The object created by a namespace import is not a standard JavaScript object literal. It is a specialized exotic object with strict internal behaviors:
  1. Live Bindings: Properties on the namespace object maintain a live connection to the exported variables in the source module. If the source module mutates an exported value, the namespace object immediately reflects that change.
  2. Property Attributes & Assignment: Exported bindings are exposed as data properties with writable: true. This is required to satisfy core JavaScript object invariants, as the values can change over time via live bindings. However, the object utilizes a custom [[Set]] internal method that always returns false. Because ES modules execute in strict mode, attempting to reassign a property throws a TypeError.
  3. Extensibility: The namespace object is non-extensible (Object.isExtensible() returns false) and sealed (Object.isSealed() returns true). It is not frozen (Object.isFrozen() returns false if the module has at least one export) because its exported data properties report writable: true.
  4. Null Prototype: The object inherits from nothing. Evaluating Object.getPrototypeOf(namespaceIdentifier) returns null.
  5. Default Export Handling: If the source module contains a default export, it is exposed on the namespace object under the explicit property key default.
  6. Symbol.toStringTag: The object has a built-in [Symbol.toStringTag] property with the string value "Module".

Mechanical Example

Source Module (source.js):
export let counter = 0;
export function increment() { counter++; }
export default class Core {}
Importing Module (main.js):
import * as SourceAPI from './source.js';

// Accessing named exports
console.log(SourceAPI.counter); // 0

// Demonstrating live bindings
SourceAPI.increment();
console.log(SourceAPI.counter); // 1

// Accessing the default export
const coreInstance = new SourceAPI.default();

// Prototype and internal characteristics
console.log(Object.getPrototypeOf(SourceAPI)); // null
console.log(Object.prototype.toString.call(SourceAPI)); // "[object Module]"
console.log(Object.isSealed(SourceAPI)); // true
console.log(Object.isFrozen(SourceAPI)); // false

// Immutability enforcement
SourceAPI.counter = 100; // TypeError: Cannot assign to read only property
SourceAPI.newProp = 'API'; // TypeError: Cannot add property newProp, object is not extensible
Master JavaScript with Deep Grasping Methodology!Learn More