繁体   English   中英

ES6 在类中使用 this 进行解构

[英]ES6 Destructuring with this inside a class

我有一个返回元组[number, number, number]的函数。 我想使用解构将元组返回的值分配给我的类变量,如下所示:

let [this.a, this.b, this.c] = functionThatReturnsTupleWithThreeNumbers()

但这不起作用。

只需删除let就可以了:

 class Foo { a; b; c; bar() { [this.a, this.b, this.c] = [1, 2, 3]; } } const foo = new Foo(); foo.bar(); console.log(foo);

更多信息在这里

打字稿游乐场

您需要在您的班级中拥有道具并将您的元组分解为它们:

class MyClass {
    a: number;
    b: number;
    c: number;

    constructor(tuple: ReadonlyArray<number>) {
        [this.a, this.b, this.c] = tuple;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM