ES6 JavaScript中的反向分配是什么?

时间:2018-06-23 01:12:13

标签: reactjs ecmascript-6 react-redux

export const startAddExpense = () => {
  return (dispatch) => {
    const {
      description = '',
      note = '',
      amount = 0,
      createdAt = 0
    } = expenseData;
    const expense = { description, note, amount, createdAt };

    database.ref('expenses').push(expense).then((ref) => {
      dispatch(addExpense(ref.key, ...expense));
    })
  }
}

export class AddExpensePage extends React.Component {
  onSubmit = (expense) => {
    this.props.startAddExpense(expense);
    this.props.history.push('/');
  };
  render() {
    return (
      <div>
        <h1>Add Expense</h1>
        <ExpenseForm
          onSubmit={this.onSubmit}
        />
      </div>
    );
  }
}

我不知道expenseData是什么。为什么等号在右边?

代码怎么说?

1 个答案:

答案 0 :(得分:3)

这称为es6中引入的destructuring assignment语法。这是一种从现有变量中提取值的方法

const foo = { bar: 0, baz: 1 } 
const { bar } = foo
console.log(bar) // 0

更老的写法是

var foo = { bar: 0, baz: 1 } 
var bar = foo.bar
console.log(bar) // 0