hookex
3/6/2020 - 5:29 AM

闭包

/**
 * 获取有顺序的颜色
 */
export const getIndexedColor = (function () {
    const len = DefaultColors.length;
    let i = 0;

    return function () {
        const current = i++;
        return DefaultColors[current % len];
    };
})();

const getRandomColor = (function() {
    const Colors = ['#4F86EC', '#D9503F', '#F2BD42', '#58A55C'];

    let i=0;
    return () => {
        i++;
        return Colors[i % Colors.length];
    }
})();