模块的整体加载

除了指定加载某个输出值,还可以使用整体加载,即用星号(*)指定一个对象,所有输出值都加载在这个对象上面。

// a.js
export function add(x, y) {
    return x + y
}
export function decrease(x, y) {
    return x - y
}
// 逐一加载
import { add, decrease } from './a'

console.log(add());
console.log(decrease());

// 整体加载
import * as c from './a'

console.log(c.add());
console.log(c.decrease());

注意,模块整体加载所在的那个对象(上例是circle),应该是可以静态分析的,所以不允许运行时改变。下面的写法都是不允许的

import * as c from './a'

// 不允许
c.add = 2
c.decrease = function() {}

results matching ""

    No results matching ""