我不明白这种语法javascript的用途

时间:2017-08-18 14:21:48

标签: javascript assignment-operator destructuring

如何在const { Types, Creators }中分配以下代码,我的意思是类型将会持有什么以及创作者将会持有什么。

   const { Types, Creators } = createActions({
        userRequest: ['username'],
        userSuccess: ['avatar'],
        userFailure: null
    })



var createActions = (function (config, options) {
  if (R.isNil(config)) {
    throw new Error('an object is required to setup types and creators');
  }
  if (R.isEmpty(config)) {
    throw new Error('empty objects are not supported');
  }

  return {
    Types: convertToTypes(config, options),
    Creators: convertToCreators(config, options)
  };
})

2 个答案:

答案 0 :(得分:3)

语法是对象解构赋值。 TypesCreators将被定义为从Types调用返回的对象返回的CreatorscreateActions()属性。例如

const {Types, Creators} = (() => {
  return {Types:0, Creators:1}
})();

console.log(Types, Creators)

答案 1 :(得分:1)

这称为destructuring assignment,它查看返回的对象并为变量分配正确的键。可以把它想象成:

Observable.next()