Reputation: 169
I have this object:
{
exports: class y {}
}
Now I'm doing JSON.stringify
on it:
JSON.stringify({
exports: class y {}
})
But it returns an empty object... What did I do wrong? Thanks
Upvotes: 0
Views: 628
Reputation: 2032
a class
is actually a function without a [[call]]
internal property. This type does not have a representation in JSON, hence the result you see. You can try to JSON.stringify
objects containing function
to see the same result.
As a rule of thumb you should never try to store code as data in JS. Storing code is hard and requires you to store lots of information about the code but also about the state
of the environment. Look at babel and its api as an example of manipulating code as data.
Upvotes: 3