简体   繁体   English

了解弯括号“ {}”的上下文

[英]Understanding the Context of Curly Brackets '{}'

I have been reviewing other people's code and while ES2015 on the whole is taking some getting use to, however, I keep on getting stuck with Destructuring. 我一直在审查其他人的代码,而总体而言,ES2015可以得到一些利用,但是,我一直沉迷于Destructuring。

Previously, In Javascript, the Curly Brackets {} were either used for blocks or objects. 以前,在Javascript中,“花括号” {}用于块或对象。 eg 例如

// Curly Brackets Block
If () {
  ...
}

// Curly Brackets in Obj
var obj = {
  a : 1,
  ...
}

However, in destructuring, we see again and again the following syntax: 但是,在解构中,我们一次又一次看到以下语法:

let a = ({ a, b }) => {
}

My question, is the arguments container an actual object or just a block? 我的问题是,参数容器是实际对象还是仅仅是块? Please explain whether the following be the same as the code above: 请说明以下内容是否与上面的代码相同:

let a = ( a, b ) => {
}

EDIT: My understanding (so far) from reading Axel Rauschmayers article on Destruturing is that we are merely mapping the props. 编辑:从阅读Axel Rauschmayers关于破坏的文章(到目前为止)我的理解是,我们只是在绘制道具。 into a new Obj always? 总是变成新的Obj? Ie: 即:

let a = { first : a, second : b } = AnObj;
===
a.a === AnObj.first;
a.b === AnObj.second;

Is the above correct? 以上正确吗? Is an obj always instantiated? obj总是实例化吗? However, that doesn't make sense as in the above function, the object thus created for the props would be an anonymous object, right? 但是,这与上面的函数没有任何意义,因此为道具创建的对象将是一个匿名对象,对吗?

Many thanks, 非常感谢,

No, the curly braces in destructuring do form neither a block nor an object literal. 不,解构过程中的花括号不会形成块或对象文字。

They definitely are not a block because they are not a statement (and don't contain a statement list), they are an expression like an object literal. 它们绝对不是一个块,因为它们不是语句(并且不包含语句列表),它们是类似于对象文字的表达式。 In fact they even do have the same syntax as an object literal, the only difference is that they are in the position of an assignment target (left hand side of an assignment operator) or a function parameter. 实际上,它们甚至具有与对象文字相同的语法,唯一的区别是它们位于分配目标(分配运算符的左侧)或函数参数的位置。

Is let a = ({ a, b }) => {…} the same as let a = ( a, b ) => {…} ? let a = ({ a, b }) => {…}let a = ( a, b ) => {…}吗?

No, really not. 不,真的不是。 Both parameter lists do declare variables a and b for the function scope, but the first function expects an object with properties .a and .b while the second function expects two arguments. 两个参数列表的确声明了函数作用域的变量ab ,但是第一个函数期望一个对象具有.a.b属性,而第二个函数期望两个参数。

My understanding is that we are merely mapping the properties into a new obj? 我的理解是,我们只是将属性映射到新的obj?

No. There is no new object created/instantiated. 否。没有创建/实例化新对象。 There is only the object that you pass in (the right hand side). 只有您传入的对象(右侧)。 And it is destructured - "pulled apart" - into pieces that are then assigned to the various sub-targets (variables, property references). 然后将其解构 (“拉开”)成块,然后分配给各个子目标(变量,属性引用)。

To write 来写

a.b = anObj.first;
a.c = anObj.second;

with a destructuring assignment you'd use 与您要使用的解构任务

({first: a.b, second: a.c}) = anObj;

(the parenthesis are necessary to distinguish the expression from a block). (括号是区分表达式与块所必需的)。

The more common use case is for variable initialisations however. 但是,更常见的用例是变量初始化。 You can shorten 你可以缩短

let b = anObj.first,
    c = anObj.second;

to

let {first: b, second: c} = anObj;

And also there's a shorthand when the variable has the same name as the property, so 当变量与属性具有相同的名称时,还有一个简写形式

let first = anObj.first,
    second = anObj.second;

is equivalent to 相当于

let {first, second} = anObj;

Is let a = { first : a, second : b } = anObj; let a = { first : a, second : b } = anObj; correct? 正确?

No, that doesn't make much sense. 不,那没有多大意义。 It would desugar to 它会去糖

let a;
a = anObj.first;
b = anObj.second;
a = anObj;

It is for destructuring: 它用于销毁:

var obj = {a: 1, b: 2},
    add = ({a, b}) => a + b;

console.log(add(obj));  //3

So basically to the statements inside the function it would appear there are 2 arguments, but when you call it you only pass an object. 因此,基本上在函数内部的语句中会出现两个参数,但是调用它时,只会传递一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM