In JavaScript is there any static way to represent the value of a instantiated class?
Below I have an two functions myDeskViaLiteral
and myDeskViaObject
function myDeskViaLiteral({laptop}) {
return laptop.status
}
function myDeskViaObject({laptop}) {
return laptop.getStatus()
}
We also have a class Laptop
:
class Laptop {
constructor ({status}) {
this.status = status
}
getStatus() {
return this.status
}
}
Now myDeskViaLiteral
as a function is quite straightforward.
let laptop = {status: 'on'}
myDeskViaLiteral({laptop})
The input and output can easily be captured / snapshot / represented as static JSON data for instance.
{
"input": [{"status": "on"}],
"output": "on"
}
But this all get more complicated when we used myDeskViaObject
and pass it a laptop
instance:
let laptop = new Laptop({status: 'on'})
myDeskViaLiteral({laptop})
In this case it's impossible (to my knowldege) to represent the input into myDeskViaLiteral
as a string or as static data.
What I'm looking for is a way represent the input and output of myDeskViaLiteral
as static json, perhaps a syntax that can declare the state
of the laptop
instance, and sort of "play it back" (in an event sort of way). Allowing us to create a snapshot of a object instance then hydrating it again.
So something like this:
{
"input": [{
"class": "Laptop",
"state": {
"status": "on"
}
}],
"output": "on"
}
Then wrap the input in a function that will create an actual class instance from static data.
Are there any standards that allow you to create a static representation of an instantiated class?