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.
Answer from slebetman on Stack OverflowIt 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.
// Create Customer class as follows:
export default class Customer {
getName() {
return 'stackoverflow';
}
}
// Import the class
// no need for .js extension in path cos it gets inferred automatically
import Customer from './path/to/Customer';
// OR
const Customer = require('./path/to/Customer')
// Use the class
var customer = new Customer();
var name = customer.getName();
How do you do to work with external js classes files and put this on a main.js file with modules?
Including JavaScript class definition from another file in Node.js - Stack Overflow
Typescript: how to import a class from a javascript file? - Stack Overflow
How to import a javascript class and extend it in another script unit
Videos
You can simply do this:
user.js
class User {
//...
}
module.exports = User // Export class
server.js
const User = require('./user.js')
let user = new User()
This is called CommonJS module.
ES Modules
Since Node.js version 14 it's possible to use ES Modules with CommonJS. Read more about it in the ESM documentation.
user.mjs ( extension is important)
export default class User {}
server.mjs
import User from './user.mjs'
let user = new User()
Using ES6, you can have user.js:
export default class User {
constructor() {
...
}
}
And then use it in server.js
const User = require('./user.js').default;
const user = new User();
There is no export default class in JavaScript. What you can do is write your JS file like this. myClass/index.js
"use strict";
class MyClass {
hello(name) {
console.log(`Hello ${name}`);
}
}
exports.default = MyClass;
Create a Type definitions for it. myClass/index.d.ts
export default class MyClass {
hello(name: string): void;
}
You can then import it into your TypeScript like this.
/// <reference path="./myClass/index.d.ts" />
import MyClass from "./myClass";
const my = new MyClass();
my.hello("Stack Overflow");
at the end of the your javascript file , write this
exports.MyClass = MyClass;
in ts files
import * as IzendaSynergy from './izenda/izenda_ui.js';
or
import { IzendaSynergy } from './izenda/izenda_ui.js';
in tsconfig.json file
"allowJs": true
You are only importing the class, but not making an instance of the class
Try
var myInstance = new Example()
myInstance.test()
If you want to call a method as a class method (without creating an object instance) you can try static methods.
You can change the file2.js as,
export default class Example {
static test() {
console.log('hello world');
}
}
then call it by using the class name in file1.js as
import Example from './file2';
console.log(Example.test());
Refer James Maa answer if you want to call it as an instance method.