【发布时间】:2018-08-24 13:48:30
【问题描述】:
如何使用 ES6 功能优化 .contact 方法。 我的线路工作良好,但正在学习 ES6 ,并且希望获得一些好的示例来编写具有某些 es6 功能的代码。 (解构或其他很酷的方式) 谢谢
因为我不喜欢而需要优化的行是:
const list_B = buttonsType.toolz.concat(buttonsType.layerz,buttonsType.categoryz,buttonsType.sheetz,buttonsType.actionz);//***line to optimize */
上下文是:
const buttonsType = {// slots Names for interactive buttons editor
toolz:['iconParentMode', 'move', 'pivot', 'scaleicon', 'skewicon', 'rotateicon', 'drawLine', 'sunNigth', 'lockAll', 'playPause',],
layerz:[ 'gb0', 'gb1', 'gb2', 'gb3', 'gb4', 'gb5', 'gb6',],
categoryz:[ 'All', 'Characteres', 'Rocks', 'Trees', 'Buildings', 'Grass', 'FurnitureINT', 'FurnitureEXT', 'Cliffs', 'Objets', 'Divers copie',],
sheetz:[ 'SpriteSheets', 'TileSheets', 'Spines', 'Sprites',],
actionz:[ 'iconRenderable', 'icon_PinGrid', 'icon_grid', 'saveIcon',],
};
//////// ┌------------------------------------------------------------------------------┐
//////// CREATE BUTTONS GRAFICS INTERACTIONS
////////└------------------------------------------------------------------------------┘
// make and store buttons Data's
(function(){
// how i can optimise for es6 and write proper this concat ?
const list_B = buttonsType.toolz.concat(buttonsType.layerz,buttonsType.categoryz,buttonsType.sheetz,buttonsType.actionz);//***line to optimize */
for (let [i,len] = [0,list_B.length]; i < len; i++) {
const name = list_B[i];
const slot = $PME.gui.skeleton.findSlot(name);
};
})();
编辑:已解决
(function(){
const list_B = Object.entries(buttonsType);
for (let [i,len] = [0,list_B.length]; i < len; i++) {
const [type,list_name] = [list_B[i][0], list_B[i][1]];
list_name.forEach(name => {
buttonsSlots[name] = $PME.gui.skeleton.findSlot(name);
buttonsSlots[name].type = type;
});
};
})();
【问题讨论】:
-
“优化”是什么意思?如果不想使用
concat,可以使用两个for ... of ...(嵌套一个)。 -
抱歉英语不是我的母语,意思是更易读,并使用 es6 功能进行优化。
-
为什么首先在对象中有这些数组?键的含义是什么?
-
正在用类别编辑器imgbox.com/tcgyYqer构建一个GUI
标签: javascript arrays ecmascript-6 concat destructuring