Categories
JavaScript Answers

How to instantiate a JavaScript class in another JavaScript file?

Spread the love

Sometimes, we want to instantiate a JavaScript class in another JavaScript file.

In this article, we’ll look at how to instantiate a JavaScript class in another JavaScript file.

How to instantiate a JavaScript class in another JavaScript file?

To instantiate a JavaScript class in another JavaScript file, we have to make sure the file with the class is loaded before we instantiate the class.

For instance, we write

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

to load file.js before we load file2.js.

file1.js has the class.

And then we can instantiate the class in file2.js.

Conclusion

To instantiate a JavaScript class in another JavaScript file, we have to make sure the file with the class is loaded before we instantiate the class.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *