![](/img/trans.png)
[英][Syntax Help]What is "variable : () => " meaning in Typescript or JavaScript
[英]what is the meaning of syntax [] in typescript
我想使用离子样本应用程序来构建一个Web应用程序。 有一部分示例代码用typescript编写。 我无法理解这个[f] = fields [f]代码的含义你能解释一下这段代码的含义吗?
这个ts文件的完整代码是:
/**
* A generic model that our Master-Detail pages list, create, and delete.
*
* Change "Item" to the noun your app will use. For example, a "Contact," or
a
* "Customer," or a "Animal," or something like that.
*
* The Items service manages creating instances of Item, so go ahead and
rename
* that something that fits your app as well.
*/
export class Item {
constructor(fields: any) {
// Quick and dirty extend/assign fields to this model
for (const f in fields) {
// @ts-ignore
this[f] = fields[f];
}
}
}
export interface Item {
[prop: string]: any;
}
您正在查看动态属性访问器。 说“fields”是{first: 1, second: 2}
,代码相当于:
this.first = fields.first this.second = fields.second
简而言之,它将字段的所有可枚举属性分配给此。
将其视为通用构造函数。 它的作用是迭代函数范围中别名“fields”的对象成员,并将值分配给“this”,这是创建的对象。 例如,如果传递的对象是:
var fields= {mem1: 1, mem2: 'marios'};
the code would then
for (const f in fields) {
this[f] = fields[f];
// 1st iteration would be
// this[mem1] = fields[mem1]; -> this[mem1] will create the property to the new object
// fields[mem1] will read the value, so it created the prop and copies the value
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.