The initial question comes from needing to do a deep copy of objects. So essentially I have it like this
Class1 {}
Class2 {}
Class3 {
class1: Class1
class2: Class2[]
}
I guess my main question is if I take the JSON from class3 and use JSON.parse will it give me an intact classes back? Would I need to use JSON.parse() as Class3? I understand JSON and I have a handle on how classes work within JavaScript.
class Dog {
name: string = "Fido";
}
class Cat {
name: string = "Sam";
}
class Combination {
dog: Dog = new Dog();
cat: Cat = new Cat();
}
let combo = new Combination();
Now, with combo, your object is represented as:
combo = {
dog: {
name: "fido"
},
cat: {
name: "sam"
}
}
if you json.stringify that, you will get a string representation of it that looks like this:
{
"dog": {
"name":"Fido"
},
"cat": {
"name":"Sam"
}
}
once you send this json object down the wire and get it back, you use json.parse to turn it from that string back into a javascript object. it's form is maintained. with json, you don't use it on classes, but only instances of classes, and it will give you back your instanced object intact, except you can't use functions, but only properties as values. For instance, if you added a method to that combo class, it would be omitted in the json representation and you would still get that json string i posted above.