Arun Kandregula
Arun Kandregula

Reputation: 665

How was I able to change imported value in ES6?

//lib-es6.js

export let counter = 3;
export function incCounter() {
  counter++;
}

From 16.7.2 section, its given that, even if we import a module via asterisk (*) the imported value cant be changed.

//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
/****************************************/    

Upvotes: 0

Views: 156

Answers (1)

Axel Rauschmayer
Axel Rauschmayer

Reputation: 25751

You are not really executing an ES module, you are executing a CommonJS module (that was transpiled from an ES module). You should get the expected result if you run this code natively:

Upvotes: 3

Related Questions