- 主题:请教一段JavaScript代码
export abstract class XXXX<T = void> {
protected progressDetail: Record<string, number> = {};
protected progressMessage: Record<string, string> | string = '';
protected progressWeight: Record<string, number> = {};
protected get progress(): ProgressArgs {
const { progressDetail: detail, progressMessage: msg, progressWeight: weight } = this; // 这句不太明白是什么语法或者语言特性
// 后续代码可直接访问detail,msg,weight等变量
// 似乎detail等价于this.progressDetail
}
}
请问这种写法:
const { progressDetail: detail, progressMessage: msg, progressWeight: weight } = this;
是JavaScript(或TypeScript)中的什么语言特性或者语法糖?有无MDN的文档介绍?
搜了一圈没有搜到
--
FROM 123.58.117.*
首先,支持了那种访问写法,包括对象、数组等。
比如 const a = {a1: 123, b1: 456};
访问a1/b1 可以写成 const {a1, b1} = a;
其次,冒号是别名
const {a1: aliasA1} = a;
相当于 const {a1} = a;
const aliasA1 = a1;
【 在 flyacat (放·逐) 的大作中提到: 】
: export abstract class XXXX<T = void> {
: protected progressDetail: Record<string, number> = {};
: protected progressMessage: Record<string, string> | string = '';
: ...................
--
修改:shaolin FROM 123.127.43.*
FROM 123.127.43.*
感谢详细的解答,讲的很明白!
顺着关键字查了下,应该是指对象的解构赋值:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring
【 在 shaolin 的大作中提到: 】
: 首先,支持了那种访问写法,包括对象、数组等。
: 比如 const a = {a1: 123, b1: 456};
: 访问a1/b1 可以写成 const {a1, b1} = a;
: ...................
--
FROM 123.58.117.*
我去,这是JS?
【 在 flyacat 的大作中提到: 】
: export abstract class XXXX<T = void> {
: protected progressDetail: Record<string, number> = {};
: protected progressMessage: Record<string, string> | string = '';
: ...................
--
FROM 183.95.135.*
typescript
【 在 a0123456789q 的大作中提到: 】
: 我去,这是JS?
:
--
FROM 220.191.18.*