Reputation: 24061
I have an entry point class called app.js. This loads various classes. In one of these classes I delcare a constant:
class Product{
constructor(){
const height = 100;
....
How can I access this variable from other classes without having to import the product class into each class that I wish to access it in?
Alternatively if this is not possible with a const how can it be done? How can I declare a var in a child class of app.js and access it in another child class of app.js without importing the class in which the vairable was declared?
Upvotes: 2
Views: 112
Reputation: 78525
You could export the const
as well as the class, but you'd need to do this at the module level rather than inside the class
constructor.
product.js
export const height = 100;
export class Product {
...
}
app.js
import {height} from "./Product";
console.log(height); // 100
If you still want height
to be a part of the Product class, I'm afraid there's no way to access it without somehow importing Product
first.
Upvotes: 3