| {}; # some code .... I've never seen = within {} that ...

问题描述 投票:-1回答:1
As mentioned in

Destructuring assignment docs

handleValueChange = (e) => {
    const { target: { id, value } = {} } = e || {}; 
    # some code ....

:= {}A variable can be assigned a default, in the case that the value unpacked from the object is const.

For example:

Here you can see
javascript const
1个回答
3
投票

is used for .In your case, if

is

while destructuring undefined, then an empty object

will be used instead.

const {a = 10, b = 5} = {a: 3};

console.log(a); // 3
console.log(b); // 5

This is needed in cases b is undefined or 5, else we will get an error like:b

Cannot read property 'id' of undefinedtargetUsing undefineddefault valuee handles this scenario:{}

我在研究一个项目时,看到了这样的模式。e我从来没有见过 undefinednull 之后

但我在网上找不到任何文档。

这种类型的语法叫什么?它的工作原理是什么?

const { target: { id, value } } = {};
console.log( id, value )

我在研究一个项目的时候,看到这样一种模式:handleValueChange = (e) => { const { target: { id, value }。 } = e

© www.soinside.com 2019 - 2024. All rights reserved.