-1

I have a JavaScript file in which I need to create an instance of a javascript class found in a different js file. Is there any way I can just "include" or "require" the file in which the Class is found? I read about using Ajax or creating a separate function to load the file, is there a more simple way?

I read here How do I include a JavaScript file in another JavaScript file? but I'm looking for something simple, as the project is too small, if I use any of these, the code for including the file will be longer than the actual code needed.

Community
  • 1
  • 1
Dimentica
  • 795
  • 2
  • 11
  • 30
  • 4
    Possible duplicate of [How to include a JavaScript file in another JavaScript file?](http://stackoverflow.com/questions/950087/how-to-include-a-javascript-file-in-another-javascript-file) – Liam Aug 18 '16 at 13:07
  • I read that :( I just wished I have not understood it and there is an easier way ... – Dimentica Aug 18 '16 at 13:08
  • 2
    you also could load the required js file before the other in your html document - definitions of methods and objects are saved in the browser after loading the script and you can access them globally (if the scope was global in your js file) – messerbill Aug 18 '16 at 13:09

1 Answers1

0

Try this way. Assume 2 have 2 js file:

file1.js

var MyClass = function() {
    // some code
    return something;
}
module.exports = MyClass;

file2.js

var MyClass = require('./file1.js');
// use MyClass

This code above run correctly when run on nodejs server.

Bui Minh Duc
  • 112
  • 1
  • 11