gpt4 book ai didi

c# - TypeScript 通过 ref 参数传递

转载 作者:搜寻专家 更新时间:2023-10-30 21:50:43 25 4
gpt4 key购买 nike

<分区>

在 C# 中,可以通过引用传递参数。例如:

    private void Add(ref Node node)
{
if (node == null)
{
node = new Node();
}
}
Add(ref this.Root);

this.Root 在执行 Add(ref this.Root)

后不会为 null

根据我从 TypeScript 中看到的情况,不可能通过引用传递参数。这段代码:

    private addAux(node: Node): void {
if (node === undefined) {
node = new Node();
}
}
addAux(this._root);

在执行addAux(this._root) 之后,this._root 仍将是未定义的,因为它的副本将被传递到addAux .

是否有一种变通方法可以在 TypeScript 中具有与 C# 中的 ref 关键字相同的功能?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com