In C# it is possible to pass parameters by reference. For instance:
private void Add(ref Node node)
{
if (node == null)
{
node = new Node();
}
}
Add(ref this.Root);
this.Root
would not be null after doing Add(ref this.Root)
From what I've seen from TypeScript, it is not possible to pass parameters by reference. This code:
private addAux(node: Node): void {
if (node === undefined) {
node = new Node();
}
}
addAux(this._root);
After doing addAux(this._root)
, this._root
will still be undefined because a copy of it will be passed into addAux
.
Is there a workaround to have the same feature as the ref
keyword from C# in TypeScript?