How to instantiate a javascript class in another js file?

The Solution to How to instantiate a javascript class in another js file? is


It depends on what environment you're running in. In a web browser you simply need to make sure that file1.js is loaded before file2.js:

<script src="file1.js"></script>
<script src="file2.js"></script>

In node.js, the recommended way is to make file1 a module then you can load it with the require function:

require('path/to/file1.js');

It's also possible to use node's module style in HTML using the require.js library.

~ Answered on 2012-10-10 06:01:11


Most Viewed Questions: