如果我将一个类导入到其他脚本中,如何在ES6语法中将参数传递给该类构造函数?
我想做这样的事情。我已经看到了各种建议,比如包装函数或使用工厂模式,但有没有更简单的方法呢?
#!/usr/bin/perl
use strict;
use warnings;
use LWP;
my $ua = LWP::UserAgent->new;
open LOG , ">download_log.txt" or die $!;
######## make sure the file with the ids/urls is in the
######## same folder as the perl script
open DLIST, "downloadlist.txt" or die $!;
my @file = <DLIST>;
foreach my $line (@file) {
#next if 0.999 > rand ;
#print "Now processing file: $line\n" ;
my ($nr, $get_file) = split /,/, $line;
chomp $get_file;
$get_file = "http://www.sec.gov/Archives/" . $get_file;
if ($get_file =~ m/([0-9|-]+).txt/ ) {
my $filename = $nr . ".txt";
open OUT, ">$filename" or die $!;
print "file $nr \n";
my $response =$ua->get($get_file);
if ($response->is_success) {
print OUT $response->content;
close OUT;
} else {
print LOG "Error in $filename - $nr \n" ;
}
}
}
答案 0 :(得分:1)
将参数传递给 es6 模块非常简单直接。 做这件简单的事。
// This is sudo code
require('./SomeClassPath.js')(param);
然后在模块文件 SomeClassPath.js 里面做这个
module.exports = function(param) {
....
}