Typescript Is there a way to pass a parameter to a method by reference?

I have a method that receives parameters, and in this method there are calculations that change the value of the parameters. When returning from a method, parameters continue to other methods for more calculations.

Is there a way to pass a parameter to a method by reference, or is the only way to attach parameters to an object and return them?

+5
source share
1 answer

With JavaScript and TypeScript, you can pass an object by reference - but not a value by reference. So paste your values ​​into the object.

So, instead of:

function foo(value1: number, value2: number) { value1++; value2++; } 

make:

 function foo(model: {property1: number; property2: number}) { model.property1++; model.property2++; // Not needed but // considered good practice. return model; } 
+9
source

Source: https://habr.com/ru/post/1243767/


All Articles