How to Instantiate a JavaScript Class in Another Js File

How to call a class from another ,js file?

I believe it's giving you that error because you imported the app.js file before class.js file. Try importing class.js first and then it should work.
example:

<body>

<div class="form-group">
<button id="submit" name="submit">Submit</button>
</div>


<script src="class.js"></script>
<script src="app.js"></script>

</body>
</html>

How to instantiate a JavaScript class in another class?

You instantiate a class from inside another class in the same way you always instantiate a class - with the keyword new.

Example:

class Foo {}

class Bar {
constructor() {
this.foo = new Foo()
}
}

bar = new Bar()

In terms of design you may find it best to 'inject' your classes dependencies. This typically leads to more testable and maintainable code. clean-code-javascript has some nice examples.

Example:

class Foo {}

class Bar {
constructor(foo) {
this.foo = foo
}
}

foo = new Foo()
bar = new Bar(foo)

How to call a class in another node.js file

Thanks to Sebastian Simon for the solution in the comments. I just thought I'll make it a formal answer.

Change car.js to

class Car {
constructor(owner){
this.owner = owner;
}
drive(){
console.log("Vroom Vroom");
}
}
module.exports = Car; // Added this

and race.js to

const Car = require('./Car.js')  // Added this
car1 = new Car("Rick Astley");
car1.drive();

Thanks again to everyone for their help :)

Including JavaScript class definition from another file in Node.js

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()

Instantiating a JS class in another JS file for an Angular Based Web App

Export your class in a separate file (Script.ts) like:

export class Foo {
someFunction() {
// your code
}
}

Import, instantiate & use it in another file's class:

 import { Foo } from './Script';

class Bar {

constructor() {
let foo = new Foo();
foo.someFunction();
}

}

How to instantiate class in another class in javascript?

Create an instance of the class, then call the instance method. You cant call an instance method from a static context like that. See the sample code below:

class ClassA {  static staticMethod() {    return 'this is static method';  }
nonStaticMethod() { return 'this is not static method'; }}
//Call static method:console.log(ClassA.staticMethod()); //Works as expected//ClassA.nonStaticMethod(); // Uncomment and see it Will cause a Uncaught TypeError
//Call instance method:const instance = new ClassA();//console.log(instance.staticMethod()); // Uncomment and see it Will cause a Uncaught TypeErrorconsole.log(instance.nonStaticMethod()); //Works as expected


Related Topics



Leave a reply



Submit