admin管理员组

文章数量:1315288

In Javascript, is there any way to import (or require) an object that's not exported by a module from that module?

I realize that this is likely bad practice, but I wonder if it's possible nevertheless. I have a Python background, so I'm confused by the idea that I can import a module but cannot access all of its data elements in Javascript.

In Javascript, is there any way to import (or require) an object that's not exported by a module from that module?

I realize that this is likely bad practice, but I wonder if it's possible nevertheless. I have a Python background, so I'm confused by the idea that I can import a module but cannot access all of its data elements in Javascript.

Share Improve this question asked Mar 28, 2016 at 14:30 Lane RettigLane Rettig 6,9585 gold badges44 silver badges54 bronze badges 8
  • 1 Even python has private variables, doesn't it? – Bergi Commented Mar 28, 2016 at 14:34
  • Yes but they're not actually private – Lane Rettig Commented Mar 28, 2016 at 14:35
  • I meant actual variables, not members (like attributes or methods). Aren't those private? – Bergi Commented Mar 28, 2016 at 14:43
  • 3 Btw, are you asking about ES6 modules, CommonJS modules or something else? – Bergi Commented Mar 28, 2016 at 14:48
  • It's a general question not specific to one module system (although I guess the answer may depend upon the system). As for Python, I'm not sure, I kinda thought everything was accessible via introspection, but maybe we should post that as a separate Python question ;) – Lane Rettig Commented Mar 28, 2016 at 15:22
 |  Show 3 more ments

1 Answer 1

Reset to default 7

Not using the module API. A module exports an object and any code importing the module is given a reference to that object ("reference" in the JS sense).

Section 15.2.3 of the spec covers exports and, very verbosely, shows that the export keyword is used to include some local variable in the module's import/export table. Quite simply, if you don't specify export, the variable is local to the module scope.

This matches the behavior of legacy IIFE modules, which used function scope to hide their local variables, then exported an object to be made public.

An ES6 module like:

export class Foo {
  ...
}

export const bar = new Foo();

after being transpiled will look something like:

(function () {
  function Foo() {
    ...
  }
  var bar = new Foo();
  return {
    Foo: Foo,
    bar: bar
  };
})();

Most articles on JS modules (even the fantastic 2ality module writeup) don't mention what happens to un-exported variables.

For parison, the CommonJS module standard states that modules have a variable exports and "the module may add its API to as it executes." The CommonJS standard has the only reference I've found so far to only exporting via keyword/object (from module context 2.1):

modules must use the "exports" object as the only means of exporting.

本文标签: Importing something that39s not explicitly exported by a module in JavascriptStack Overflow