在JavaScript ES6上制作相关字符串的最佳表格?

时间:2015-07-04 03:18:29

标签: javascript gulp ecmascript-6 object-literal

我使用gulp来管理我的所有网站资产。我将所有代码迁移到ES6。您可以在此处查看我的初学者gulpfile:https://github.com/ramasilveyra/solid-webapp/blob/master/gulpfile.babel.js

我怀疑这是否是处理所有不同资产路径的最佳方式。有一个更好的形式(没有功能更实用)?谢谢!

const paths = {
    src: './src',
    dist: './dist',
    assets: {
        src: paths.src + '/assets',
        dist: paths.dist + '/assets'
    },
    scripts: {
        src: () => paths.assets.src + '/scripts',
        dist: () => paths.assets.dist + '/scripts'
    },
    styles: {
        src: () => paths.assets.src + '/styles',
        dist: () => paths.assets.dist + '/styles'
    },
    media: {
        src: () => paths.assets.src + '/media',
        dist: () => paths.assets.dist + '/media'
    },
    fonts: {
        src: () => paths.assets.src + '/fonts',
        dist: () => paths.assets.dist + '/fonts'
    }
};

1 个答案:

答案 0 :(得分:0)

ES6不会为您带来新的东西。但是你可以将填充对象分成几个语句:

const paths = {
   src: './src',
   dist: './dist'
};
paths.assets = {
    src: paths.src + '/assets',
    dist: paths.dist + '/assets'
};
paths.scripts = {
    src: () => paths.assets.src + '/scripts',
    dist: () => paths.assets.dist + '/scripts'
};
// ... and so on

请注意,可以在进一步的操作中修改通过const定义的对象。如果您想要阻止更改其任何字段,请在分配后致电Object.freeze(paths)