I don't understand what's going on this syntax javascript [duplicate]

隐身守侯 提交于 2019-12-13 03:44:16

问题


How gonna assign in const { Types, Creators } in the below code I mean what Types gonna hold and what Creators gonna hold.

   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)
  };
})

回答1:


The syntax is object destructuring assignment. Types and Creators will be defined as the Types and Creators properties returned from the object returned at createActions() call. For example

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

console.log(Types, Creators)



回答2:


This is called a destructuring assignment, It looks at the returned object and assigns the correct key to the variable. Think of it as shorthand for:

const 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)
  };
})

let results = createActions({
        userRequest: ['username'],
        userSuccess: ['avatar'],
        userFailure: null
    }),
    Types = results.Types,
    Creators = results.Creators;


来源:https://stackoverflow.com/questions/45758970/i-dont-understand-whats-going-on-this-syntax-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!