JavaScript DynamicObject

I have seen posts a long while back, but nothing recently. If I have a DynamicObject registered with the JavascriptEngine and use the following JS code:
var myVar = new MyDynamicObject();

in c++ myFunction the following code is always null:
auto* thisObject = dynamic_cast< MyDynamicObject*> (args.thisObject.getObject())

However if I call it using MyDynamicObject.myFunction();
it works great.
What is a way to be able to set a bunch of variables on an object (DynamicObject) and then call a function to use these variables?