Why can't there be a cc.loader.loadres in the constructor?

Why can't there be a cc.loader.loadres in the constructor?

I made a ts script which doesn’t extent cc.component and has a ctor function. now, I had a cc.loader.loadres closure function in the ctor function where I was reading a json file and parsing it. when I was doing this I was getting an error : Uncaught SyntaxError: Unexpected end of JSON input

when I moved the loadres to another function and called that function after instantiating the ts class, it was working fine.

I want to know why this happened.


it seems had some errors with your json file, I made a class not extend cc.component with typescript, and call cc.loader.loadRes in actor function to read a json asset.
it works well, and I can find any error as you mentioned.
NewProject_1.zip (842.4 KB)
or can you provide a demo?

thanks for the response. I guess what I was doing wrong was that when declaring a variable of type TestClass, I was doing :

obj : any = null;

I should have been doing :

obj: TestClass = null;

I don’t know if this was the problem but my code works now.

thanks again!