我的问题最好通过这个jsfiddle,其代码如下:
var a = 1, b = 'x', c = true;
var d = {a: a, b: b, c: c}; // <--- object literal
var e = [a, b, c]; // <--- array
var f = {a, b, c}; // <--- what exactly is this??
// these all give the same output:
alert(d.a + ', ' + d.b + ', ' + d.c );
alert(e[0] + ', ' + e[1] + ', ' + e[2]);
alert(f.a + ', …Run Code Online (Sandbox Code Playgroud) 在检查Alfresco ADF的内容元数据组件时,我偶然发现了以下打字稿功能,但我无法理解它:
private saveNode({ changed: nodeBody }): Observable<Node> {
return this.nodesApiService.updateNode(this.node.id, nodeBody);
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是{ changed: nodeBody }.
根据这个和这个答案,大括号用于表示对象文字,作为使用键/值对作为函数参数的一种方式。但在这里它被用作参数。如果这创建了一个对象,在我的理解中,这意味着它changed是其属性的名称,并nodeBody指的是属性值。但是这个对象分配给了哪个变量,如何在方法体中引用它?
更让我困惑的是,只有nodeBody在 return 语句中使用。那么为什么不立即将其用作单个参数呢?
这种输入形式的好处或用例是什么?