我写的这个东西,怎么改成对象形式?

var l = [];

for (i=0; i<app.activeDocument.layers.length;i++)  
{
    l[i] =[0,0,"0"] ;

    var a = app.activeDocument.layers[i];
    var ybl = new ybLayer( a ) ;    //( 图层对象 )

    l[i][0] = ybl.ybLeftTopY;
    l[i][1] = ybl.ybHeight;
    l[i][2] = app.activeDocument.layers[i].name ;

}  
阅读 1.8k
1 个回答

别闹,程序猿哪儿来的对象。。。

看看你的代码。。。该减肥了。。。

var l = [];

for (i=0; i<app.activeDocument.layers.length;i++)  
{
    l[i] =[0,0,"0"] ;

    var a = app.activeDocument.layers[i];
    var ybl = new ybLayer( a ) ;    //( 图层对象 )

    l[i][0] = ybl.ybLeftTopY;
    l[i][1] = ybl.ybHeight;
    l[i][2] = app.activeDocument.layers[i].name ;

} 

先把冗余的部分去一去:

var l = [];
for (i=0; i<app.activeDocument.layers.length;i++) {
    l[i] =[] ;
    
    var a = app.activeDocument.layers[i];
    var ybl = new ybLayer( a ) ;    //( 图层对象 )

    l[i][0] = ybl.ybLeftTopY;
    l[i][1] = ybl.ybHeight;
    l[i][2] = a.name ;
} 

再把重复的赋值合并一下:

var l = [];
for (i=0; i<app.activeDocument.layers.length;i++) {    
    var a = app.activeDocument.layers[i];
    var ybl = new ybLayer( a ) ;    //( 图层对象 )

    l[i] = [ybl.ybLeftTopY, ybl.ybHeight, a.name]
} 

然后整个容:

var l = app.activeDocument.layers.map(el => {
    var ybl = new ybLayer(el);
    return [ybl.ybLeftTopY, ybl.ybHeight, a.name];
});

对象还是没有,但是已经朝面向函数编程迈出了一小步。。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题