//lib-es6.js
export let counter = 3;
export function incCounter() {
counter++;
}
根据16.7.2 section,即使我们通过星号(*)导入模块,导入的值也无法更改。
//main-es6.js
import * as lib from './lib-es6'; // imported via asterisk(*)
// The imported value `counter` is live
console.log(lib.counter); // 3 . => I expected this
lib.incCounter();
console.log(lib.counter); // 4 . => I expected this
/****************************************/
// But I was able to change lib.counter.
// Question: Can we change imported value in ES6 if we import it via asterisk (*)?
lib.counter++; // Not an error. ==> I DID NOT expected this.
console.log(lib.counter); // 5
/****************************************/
答案 0 :(得分:3)
您实际上不是在执行ES模块,而是在执行CommonJS模块(从ES模块转译而来)。如果您本机运行此代码,则应获得预期的结果:
--experimental-modules
的{{3}}的Node.js 12上