I want to create a class in one file, import and extend it in another. Something like:
/* someclass.js */
export class SomeClass {}
/* somesubclass.js */
import SomeClass from './someclass'
class SomeSubClass extends SomeClass {}
Is it possible or is there any better way to do this (e.g. using node.js module.exports
)?
Your example is almost correct. You can export your class as default:
export default class SomeClass {}
// and then import
import SomeClass from './someclass';
class SomeSubClass extends SomeClass {}
or use named export:
export class SomeClass {}
// and then import
import { SomeClass } from './someclass';
class SomeSubClass extends SomeClass {}
See export syntax on MDN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With