Lee Goddard
Lee Goddard

Reputation: 11183

ECMA double fat arrow use?

Can you explain this code? Is it currying?

    export const thing = (...items) => (wotsit) => {
        const thing = (props, {enums}) => {
            // ...
        };

        thing.contextTypes = {
            enums: PropTypes.object
        };

        return thing;
    };

    export default thing;

Upvotes: 1

Views: 375

Answers (2)

Danziger
Danziger

Reputation: 21191

Yes, it is. Without the arrow functions, it will look like this:

export const thing = function(...items) {

    // `items` will be an array with all the arguments that you pass in.

    return function(wotsit) {
        const thing = function(props, {enums}) {
            // ...
        };

        thing.contextTypes = {
            enums: PropTypes.object
        };

        return thing;
    };
};

export default thing;

On the other hand, this:

const thing = (props, {enums}) => {
    // ...
};

Is using parameters destructuring. It's the same as this:

const thing = (props, options) => {
    let enums = options.enums;
};

Upvotes: 4

KevBot
KevBot

Reputation: 18908

It is returning back a function to be called at a later time. If the functions were not in ES6, it would look something like:

function thing(a, b) {
    return function(wotsit) {
        const thing = {};
        ...
        return thing;
    }
}

Which would eventually work something like this:

let a = thing(1, 2);
let b = a(wotsit); // Gives you back thing object

Upvotes: 1

Related Questions