Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

javascript - ES6在类内部进行销毁(ES6 Destructuring with this inside a class)

I have a function that returns a tuple [number, number, number] .(我有一个返回元组[number, number, number]的函数。)

I want to use destructuring to assign to my class variables the values returned by the tuple like this:(我想使用解构将元组返回的值分配给我的类变量,如下所示:)
let [this.a, this.b, this.c] = functionThatReturnsTupleWithThreeNumbers()

but this doesn't work.(但这行不通。)

  ask by kaki6876 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just remove the let and you're good to go:(只需删除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); 

More info here(更多信息在这里)

Typescript playground(打字稿游乐场)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...