ECMA双脂箭使用?

时间:2017-05-10 06:09:49

标签: javascript syntax ecmascript-6

你能解释一下这段代码吗?它在干嘛?

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

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

        return thing;
    };

    export default thing;

2 个答案:

答案 0 :(得分:4)

是的,确实如此。如果没有arrow functions,它将如下所示:

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;

另一方面,这个:

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

使用参数destructuring。它与此相同:

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

答案 1 :(得分:1)

它返回一个稍后要调用的函数。如果函数不在ES6中,它看起来像:

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

最终会有这样的事情:

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