0

I'm using the code below to get access to variables in variables.js from main.js:

variables.js:

export default {
    bla: 123,
}

main.js:

var vars = require('./variables').default;
class MyClass extends blabla {
  constructor (){
    super();
  }

  myFunction (){
    console.log(vars.aa)
  }
}

in main.js to get access to variable I have to use vars.bla.

my question is How can I get access to it just with bla(yea to lazy)

Adrin
  • 543
  • 3
  • 10
  • https://stackoverflow.com/a/3244414/10060553 Maybe check this out? This might be what you're looking for. – ecorp Aug 29 '18 at 18:39
  • Are you sure you want to do that? Could be a bad idea if you have the same variable name in both files. I think there's no way to do that, anyways. You'll have to make the variables manually. You could use destructuring. https://stackoverflow.com/questions/3422458/unpacking-array-into-separate-variables-in-javascript – Phiter Aug 29 '18 at 18:41

2 Answers2

2

You can access it by using destructuring assignment like this:

let { bla } = require("./variables").default

console.log(bla)

https://codesandbox.io/s/nnqw0pynk4

dziraf
  • 3,117
  • 1
  • 12
  • 20
1

variables.js:

module.exports = {
  bla:123
}

Other files:

var {bla} = require('./variables');
console.log(bla)    
Cody G
  • 6,752
  • 2
  • 28
  • 40