私は無名関数を使用してファイルを必要とし、パラメータを渡すことによって、別のnodejsファイルからオブジェクトをパラメータを持つmodule.exportsはしてエクスポートした自己実行匿名関数を呼び出すようにしようとしているが、このようなオブジェクト:自己実行module.exported匿名ノード関数に引数を渡すにはどうすればよいですか?
fileThatCallsTheAnonymousSelfExecutingFunctionを: `
var parameters = {
filename: "UsernamesAndQueues",
headers: ["username", "password", "queue"],
columns: ["ascii", "ascii", "ascii"],
wordLength: 25,
count: 100
};
var generateData = require("../scripts/generateData.js")(parameters);
generateData.js:
// if the function does not call itself in this file, i can pass the
// arguments normally, and it prints out the expected options.
module.exports = (function (options) {
console.log("Hello %s", JSON.stringify(options));
options = options || {
"headers": ["username", "password", "queueName"],
"columns": ["ascii", "ascii", "ascii"],
"wordLength": 26,
"count": 101
};
console.log("Current options are: %s", JSON.stringify(options));
// I just want to know what the arguments are for now, so I interrupt
process.exit();
// i just want for the moment to print out the options object
// later on, i would like to use the options as i would parse arguments ??? options[0], options[1], options[2] etc.
// rest of the file that really needs the function arguments
console.log("something");
var fs = require('fs'),
generate = require('csv-generate'),
filename = "UsernamesAndQueues",
csvfilestream = fs.createWriteStream("../data/" + "generated" + filename + ".csv", { flags: 'w' }),
error = fs.createWriteStream(__dirname + '/node.error.log', {flags: 'w'}),
// CSV generator (read command line arguments, create with defaults, making filename required)
generator = generate({
header: options.headers,
columns: options.columns,
max_word_length: options.wordLength,
length: options.count
});
// Redirect stdout/stderr to file
process.stdout.write = csvfilestream.write.bind(csvfilestream);
process.stderr.write = error.write.bind(error);
// Handle Uncaught Exceptions/Errors
process.on('uncaughtException', function(err){
console.error((err && err.stack) ? err.stack : err);
});
// Output CSV to stdout stream -> to file
generator.pipe(process.stdout);
})(arguments[0]);
私は関数に渡された引数を正しく処理する方法は何ですか?私はコマンドライン引数から(オプションの)コマンドライン引数を使って、このようにしてcmdラインから呼び出すこともできます。私はこのようにする方法を見つけた後、さらに引数パーザを使うことができます。
コマンドラインスクリプト(node generateData.js --headers ["param1", "param2", "param3"] --columns ["ascii", "ascii", "ascii"] --wordLength 25 --count 100
)として実行できるノードスクリプト(関数)を実装するための他の提案がある場合は、モジュールが別のモジュールに関数ファイルをエクスポートし、上記のようなパラメータオブジェクト
その他の質問:私は正しく何をしていますか、間違っていますか、これらの概念をよりよく理解するために何をどこで読むことができますか?私はMozilla Devと他のサイトを見てきました。パラメータを使って自己実行する匿名関数に関するものを見つけましたが、外部ファイルから呼び出されたものはありませんでした。おそらく、私は正しいものを探していないかもしれません...
後で編集: 私はすでに関数をエクスポートし、別のファイルから呼び出してみました。これは簡単な方法です。
scriptThatRequiresTheExportedModuleAndCallsItWithParameters(引数)あなたは適切な構造を持っていない
var parameters = {
headers: ["username", "password", "queue"],
columns: ["ascii", "ascii", "ascii"],
word_length: 25,
count: 100
};
// this works
var generateUsernames = require('../scripts/generateData.js')(parameters);
generateData.js
// this works, just exporting the function, without self-executing (calling)
// i call the function (providing arguments) from the other file requiring it
module.exports = function (options) {
console.log("Hello %s", JSON.stringify(options));
options = options || {
"headers": ["username", "password", "queueName"],
"columns": ["ascii", "ascii", "ascii"],
"wordLength": 26,
"count": 101
};
console.log("Current options are: %s", JSON.stringify(options));
// exiting, because, for now, i just want to know what the arguments (options) are
process.exit();
console.log("something");
var fs = require('fs'),
generate = require('csv-generate'),
filename = "UsernamesAndQueues",
csvfilestream = fs.createWriteStream("../data/" + "generated" + filename + ".csv", { flags: 'w' }),
error = fs.createWriteStream(__dirname + '/node.error.log', {flags: 'w'}),
// CSV generator (read command line arguments, create with defaults, making filename required)
generator = generate({
header: options.headers,
columns: options.columns,
max_word_length: options.wordLength,
length: options.count
});
// Redirect stdout/stderr to file
process.stdout.write = csvfilestream.write.bind(csvfilestream);
process.stderr.write = error.write.bind(error);
// Handle Uncaught Exceptions/Errors
process.on('uncaughtException', function(err){
console.error((err && err.stack) ? err.stack : err);
});
// Output CSV to stdout stream -> to file
generator.pipe(process.stdout);
};
ニットピッキング:それは*自己実行*ではなく、インラインで呼び出されただけです。それに対する口語の言葉は、IIFE(インラインで呼び出される関数式)です。 –
@ T.J.Crowder私はそれが*即座に*呼び出された関数式であると思った。 –
@torazaburo:私は両方のことを聞いたことがありますが、私が好きで使いたいものです。 :-) –