As someone who is just starting out in the Typescript and Angular-2 world, my previous experience includes working with Java and Angular-1.5.
Imagine a scenario where there is a component class with several variables that need to be used across functions, either from a JSON response or generated after some calculations based on JSON data.
Is it better to define these variables as class-level members like this:
processFunction(){ .... .... this.someVar = this.someService.getData(); .... .... this.functionA(); //funcA needs this.someVar this.functionB(); //funcB needs this.someVar }
Or should the variables be saved as function scope members and passed as arguments like this:
processFunction(){ .... .... let someVar = this.someService.getData(); .... .... this.functionA(someVar); this.functionB(someVar); }
Which approach is the most suitable to follow?
PS: The provided code is a simplified version of a broader use case where multiple variables need to be defined as function variables (using let) and passed as function arguments.
In a Java context, the best practice would typically involve using class-level variables when more than 2 functions share variables.
I would appreciate suggestions specifically related to handling such scenarios in Typescript.