2

In Java, you can do something like this:

import javax.swing.*

And then, without declaring JButton anywhere, you can do:

JButton button = new JButton();

So a single import statement automatically makes everything under javax.swing. a locally accessible variable. This is quite nice and saves a lot of typing. One import statement gives you everything.

With Node JS, my understanding is you would have to do something like:

var swing = require('javax.swing');

And then if you want properties of swing available as variables you would need to do:

var JButton = swing.JButton;

And then you can finally do:

var button = new JButton();

Is there anyway to automatically create locally scoped variables from imported files with a single require statement in Node JS?

Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177

2 Answers2

1

You can simply do

var JButton = require('javax.swing').JButton;
var button = new JButton();
baao
  • 71,625
  • 17
  • 143
  • 203
0

In addition to the other answer, if you are willing to use ES6 and transpile with Babel, you can use ES6 modules (they're due for implementation in V8, which means Node.js would get them relatively soon too).

This would give you something like this:

import { JButton } from 'javax.swing';
Zlatko
  • 18,936
  • 14
  • 70
  • 123