prototype casting (Object.assign)

Haai :slight_smile: ,

I saved an object in JSON format (and kept it in sessionstorage). After restoring my object, only the fields are accessible. So when I try to call a member method of my object, I get this error: "Uncaught TypeError: this.FCalculator.CalculateProbabilities is not a function ". How can I cast this object so that it can use its functions?

My code:

(FCalculator is a class member of type TProbabilityCalculator)

procedure TfrmExpert.RestoreExpertState;
  oStateObject, oExpertStateObject: TJSObject;
oExpertStateObject := TJSObject(oStateObject[cStateObjState]);
FCalculator := TProbabilityCalculator(oExpertStateObject['FCalculator']);

Best regards,

I'm not sure I understand the full scope of what you are trying to do.
When you persist an object in JSON format (I'm not sure via what code you do this?), what happens is that the published properties get streamed into a JSON object. This JSON object is at that time nothing more & nothing less than a JSON representation of the published properties and this JSON object has relationship nor connection with the Pascal class of which it has streamed the properties.
When you want to restore, you'd need to create an instance of the Pascal class and stream published properties back from the JSON object but you cannot cast the JSON object to a Pascal class instance.