An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
July 23, 2022 02:16 pm GMT
Original Link: https://dev.to/nucleoid/storing-classes-and-objects-4inc
Storing Classes and Objects
Class
First thing first, classes require to be registered before used in Nucleoid:
class Order { constructor(item, qty) { this.item = item; this.qty = qty; }}nucleoid.register(Order);
Objects
The same thing for objects, once initiated and assigned to the var
variable as well as it stored.
app.post("/orders", () => { var order = new Order("ITEM-123", 3); return order;});
and it can retrieve by its var
variable as mentioned earlier.
app.get("/orders", () => { return order;});
{ "id": "order0", "item": "ITEM-123", "qty": 1}
if object initiated without assigning var
variable, the runtime automatically assign var
variable along with id
app.post("/test", () => new Order("ITEM-123", 3));
{ "id": "order0", "item": "ITEM-123", "qty": 1}
id
of object is always the same to its globalvar
so that either can be used to retrieve the object likeOrder["order0"]
andorder0
.
if object assigned to either let
or const
, the runtime will create var
variable the same as its id
app.post("/orders", () => { const order = new Order("ITEM-123", 3); return order;});
{ "id": "order1", "item": "ITEM-123", "qty": 1}
Now, it can use its id
as var
in order to retrieve the object
app.get("/test", () => order1);
Original Link: https://dev.to/nucleoid/storing-classes-and-objects-4inc
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To