es6 - import all named module without alias

前端 未结 5 797
执笔经年
执笔经年 2020-12-03 00:11

I know that we can import all named modules with alias as below,

import * as name from \"module-name\";

Ref: https://developer.mozilla.org

5条回答
  •  渐次进展
    2020-12-03 00:52

    global is your current scope in node.js, similar to the window object in the browser, so you can import into this object.

    To import all symbols from util module:

    import * as util from "./util";
    util.importAll(util, global);
    

    In util.js:

    /**
     * Takes all functions/objects from |sourceScope|
     * and adds them to |targetScope|.
     */
    function importAll(sourceScope, targetScope) {
      for (let name in sourceScope) {
        targetScope[name] = sourceScope[name];
      }
    }
    

    ... and a number of other functions like assert() etc., which I need everywhere, and which should be part of the JavaScript language, but are not yet. But as others said, use this sparingly.

提交回复
热议问题