函数数组 - Javascript

时间:2018-05-09 18:03:15

标签: javascript vuejs2

我正在开发一个vue-based系统,对于我的桌子,我正在使用它:https://github.com/matfish2/vue-tables-2

该组件有很多选项。可以定义自定义单元格模板以在显示数据之前对其进行操作,例如“created_at”列中的数据:

 templates: {
                created_at(h, row) {                   
                  return this.formatDate(row.created_at);
                },    
              //(...)                      
            }

我也可以添加其他模板:

var user_id = function(h,row){return row.id};
...

templates: {
                created_at(h, row) {                   
                  return this.formatDate(row.created_at);
                },    
              user_id,                  
              (...) // etc

            }

但是我想将函数分组到一个变量中,所以我可以重用代码,比如:

 var custom_template = [
        { user_id(h,row){return row.id} },
        { user_name(h,row){return row.name} },
        { created_at(h, row){return this.formatDate(row.created_at)} }                 
    ];

 templates: {                
            custom_templates                                                    
        }

编辑1: 所以我可以有类似的东西:

templates: {                
          user_id(h,row){return row.id} ,
          user_name(h,row){return row.name} ,
          created_at(h, row){return this.formatDate(row.created_at)}                                                    
        }

它不起作用,但是可能吗?我需要额外的代码来实现这一目标吗? 谢谢! =)

2 个答案:

答案 0 :(得分:3)



#!/usr/bin/perl

use strict;
use warnings;

$|=1;

my $filea = $ARGV[0];
my $fileb = $ARGV[1];


open ( FA, '<', $filea) || die ( "File $filea Not Found!" );
open ( FB, '<', $fileb) || die ( "File $fileb Not Found!" );
#open ( FC, ">", $filec) || die ( "File $filec Not Found!" );

my %hash;
while ( <FB> ) {
    chomp;
    my($dist, $sec, $cls, $max) = split ",";
    push @{ $hash{$dist, $sec, $cls} }, $max;
}

while ( <FA> ) {
    chomp;
    my($dist, $sec, $cls, $add, $idx, $qtd) = split ",";

    for my $max ( @{ $hash{$dist, $sec, $cls } }){
        my $tot = $qtd -1;
        print join(",", $dist, $sec, $cls, $add, $idx, $tot)."\n";
         $qtd=$tot;

    }
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你需要结合Igor的解决方案,以某种形式扩展数组来创建函数数组,例如spread syntax