Multiple classes in a module ok? #1246
Unanswered
espruino-discuss2
asked this question in
Tutorials
Replies: 2 comments
-
Posted at 2023-02-13 by @thyttan Hi! You can look to the existing modules for examples. Particularly widget_utils maybe, then you can also look at it's corresponding tutorial. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Posted at 2023-02-13 by MarcEvan Thanks! I actually just found the answer. You have to export your classes with names assigned: module.exports = {Dog: Dog, Cat: Cat}; and then do this in your referencing code: var Dog = require("animals").Dog; var c = new Cat(); |
Beta Was this translation helpful? Give feedback.
0 replies
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
Posted at 2023-02-13 by MarcEvan
Can you specify multiple classes in a module? If so, how do
you reference them individually after a requires() invocation?
--- modules/animals.js ---
class Animal { }
class Dog extends Animal { }
class Cat extends Animal { }
exports = Dog, Cat;
var animals = require("animals");
var d = new Dog();
var c = new Cat();
Uncaught ReferenceError: "Dog" is not defined
at line 2 col 5
var d=new Dog();
Uncaught ReferenceError: "Cat" is not defined
at line 3 col 5
var c=new Cat();
Beta Was this translation helpful? Give feedback.
All reactions