问题
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