Object destructuring syntax - ES6

前端 未结 2 985
臣服心动
臣服心动 2021-01-17 06:21

I had been through array destructuring syntax, which is well understood.

What exactly are we doing below, when we say var {p, q} = o;?

Is

2条回答
  •  自闭症患者
    2021-01-17 06:58

    That's because using your syntax for object destructing, the names used in the LHS expression ({a, b}) are used as keys into the RHS expression (o). Since a and b are not properties of o, that fails (returns undefined).

    The relevant section in the spec for this is in Runtime Semantics: DestructingAssignmentEvaluation under AssignmentProperty : IdentifierReference Initializer (2nd from last). AssignmentProperty are your a (and b... separately), and. The StringValue of a is 'a' and that is used as a key to get a value from o (equivalent o['a'] in this case).

    It would work if you'd do:

    var {p:a, q:b} = o;
    

    which uses AssignmentProperty : PropertyName : AssignmentElement (last entry) which uses a propery name (p) AND an assignment element (a).

提交回复
热议问题