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…


This content originally appeared on DEV Community and was authored by Can Mingir

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 global var so that either can be used to retrieve the object like
Order["order0"] and order0.

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);


This content originally appeared on DEV Community and was authored by Can Mingir


Print Share Comment Cite Upload Translate Updates
APA

Can Mingir | Sciencx (2022-07-23T14:16:00+00:00) Storing Classes and Objects. Retrieved from https://www.scien.cx/2022/07/23/storing-classes-and-objects/

MLA
" » Storing Classes and Objects." Can Mingir | Sciencx - Saturday July 23, 2022, https://www.scien.cx/2022/07/23/storing-classes-and-objects/
HARVARD
Can Mingir | Sciencx Saturday July 23, 2022 » Storing Classes and Objects., viewed ,<https://www.scien.cx/2022/07/23/storing-classes-and-objects/>
VANCOUVER
Can Mingir | Sciencx - » Storing Classes and Objects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/23/storing-classes-and-objects/
CHICAGO
" » Storing Classes and Objects." Can Mingir | Sciencx - Accessed . https://www.scien.cx/2022/07/23/storing-classes-and-objects/
IEEE
" » Storing Classes and Objects." Can Mingir | Sciencx [Online]. Available: https://www.scien.cx/2022/07/23/storing-classes-and-objects/. [Accessed: ]
rf:citation
» Storing Classes and Objects | Can Mingir | Sciencx | https://www.scien.cx/2022/07/23/storing-classes-and-objects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.