Skip to content Skip to sidebar Skip to footer

Es6 Variable Import By Reference Or Copy

Suppose i have a var.js export let x = 1; export const f = () => x = 5; Then i execute this in another file import { x, f } from './var.js'; console.log(x); // 1 f(); console.l

Solution 1:

ES6 import/exports are actually bindings (references). As the value of x in original file var.js changes, it's reflected in another file too.

Reference: http://2ality.com/2015/07/es6-module-exports.html

Solution 2:

solution doesn't work for functions

exportlete = () => {
  console.log('b')
}

window.b = () => {
  e = () => {
    console.log('c')
  }
}

the when calling from another file, the "reference" doesn't change.

import { e } from'./test'e() // bb()
e() // still b

Post a Comment for "Es6 Variable Import By Reference Or Copy"