Typescript 解构时传递的访问实例
我在TypeScript中发现了解构功能,它看起来很省时。但是,我想知道在使用解构时如何引用传递给函数的实例。示例如下:Typescript 解构时传递的访问实例,typescript,Typescript,我在TypeScript中发现了解构功能,它看起来很省时。但是,我想知道在使用解构时如何引用传递给函数的实例。示例如下: function fn({foo, bar}) { // access the A instance passed to function? } class A { constructor(public foo: number, public bar: number) {}; } fn(new A(1,9)); 我真的需要查看参数列表吗,还是我遗漏了什么?您
function fn({foo, bar}) {
// access the A instance passed to function?
}
class A {
constructor(public foo: number, public bar: number) {};
}
fn(new A(1,9));
我真的需要查看参数列表吗,还是我遗漏了什么?您可以在方法内部使用解构,而不是作为参数,享受这两个世界
function fn(a: A){
var {foo, bar} = a;
}
class A{
foo:number;
bar:number;
}
fn(new A());
寻找它传递给什么,这样你就可以知道它是如何工作的,它不是魔法。
下面是一个用于Typescript解构的示例也许您应该在一步之后使用解构
function fn(a: A)
{
let {foo, bar} = a;
//You have both destruted foo, bar and original a
}
好吧,这个想法是为了避免a.foo-我不知道这个解决方案是如何利用解构的?我的问题是它不允许默认参数值。有人知道两者兼得的方法吗?