How to instantiate a javascript class in another js file?

Import a Class from another file using JavaScript

 

Class Cars

class cars {
 constructor(id, name, color) {
   this.id = id;
   this.name = 'AUDI';
   this.color = 'red';
 }
 color() {
   this.color += 100;
 }
}
export {cars};


// another js file
import {cars} from './another-file.js';
const car = new cars(1, 'AUDI', 'red');
console.log(car.id); 
console.log(car.name);
car.color();
console.log(car.color); 

Tags:

Share:

Related posts