【发布时间】:2018-12-11 14:26:24
【问题描述】:
我想使用 ionic 示例应用来构建网络应用。有一部分示例代码是用打字稿编写的。我无法理解代码的含义 这[f] = 字段[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;
}
【问题讨论】:
-
在
for in循环的上下文中,f是fields对象的标识符键,this[key] = fields[key]正在将值从fields复制到this。
标签: javascript angularjs typescript ionic-framework