2

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 myDeskViaObjectand 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?

ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • I'm not sure exactly what you're asking, nor why you want to do this. It sounds like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), but it might just be that you need to override [`toJSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior). Can you elaborate on why you think you need to modify the object before passing it to the method? – zzzzBov Sep 07 '17 at 19:55
  • @zzzzBov I'm not looking to modify the object, I just want to be able to snapshot an object then reassemble it from scratch. – ThomasReggi Sep 07 '17 at 19:58
  • @zzzzBov it's not an XY problem I'm treading on the boundaries of what the language allows, and I'm curious if there are standards in the space. – ThomasReggi Sep 07 '17 at 19:59
  • You cannot serialise classes, and just taking their name is potentially unsafe. I recommend using a [`fromJSON` function that knows the structure](https://stackoverflow.com/a/11810861/1048572) together with `toJSON`. – Bergi Sep 07 '17 at 20:03
  • 1
    duplicate of [Find a java script library for serialization & deserialization objects in JavaScript with property support](https://stackoverflow.com/q/21287687/1048572)? – Bergi Sep 07 '17 at 20:04
  • 2
    Please remove the request for a library from your question, that would make it off-topic. – Bergi Sep 07 '17 at 20:05

0 Answers0