1

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;
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
  • `=>` that lambda used for anonymous function, `export` are used to export function, variables which can be imported by other files – Shekhar Pankaj May 10 '17 at 06:17

2 Answers2

4

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;
};
Danziger
  • 19,628
  • 4
  • 53
  • 83
  • Shouldn't we use `bind` here, as you haven't used fat arrows in your code? – bharadhwaj May 10 '17 at 06:25
  • 1
    Just if you are using `this` inside any of those functions. See http://stackoverflow.com/questions/26477245/when-to-use-bind-in-js – Danziger May 10 '17 at 06:28
  • 1
    I guess it's the implicit return that threw me -- so much ECMA has all the legibility of early 1990's Perl code. The Perl community made implicit returns (and much else) socially unacceptable, I hope the ECMA peeps follow suit soon. Thanks for your help. – Lee Goddard May 10 '17 at 09:28
1

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
KevBot
  • 17,900
  • 5
  • 50
  • 68