Answer the question
In order to leave comments, you need to log in
How to import a class in javascript?
Hello. I searched on the Internet, but did not understand.
I have the most common class. The object to be created from another file
Class:
class Shop {
#products = []
addProducts(products)
{
var newProducts = [];
products.forEach(function (product) {
newProducts[product["id"]] = new product(
product["id"],
product["name"],
product["description"],
product["price"]
);
});
this.#products = this.#products.concat(newProducts);
return this;
}
getProduct(id)
{
return this.#products[id];
}
}
module.exports.Shop = Shop;
Answer the question
In order to leave comments, you need to log in
const {Shop} = require('Shop');
const shop = new Shop();
product["id"], product["name"]
can write instead product.id, product.name
, the fields are known in advance, there is no need to complicate {}
, not an array []
, since the keys are arbitrary id, the #products field also doesn’t really look like an array Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question