![](/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.