我试图通过要求与匿名函数的文件和传递的参数来调用带有一个参数module.exports导出的自执行匿名函数从另一个文件的NodeJS Object对象是这样的:如何将参数传递给自执行的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
并作为所需的模块导出函数文件到另一个运行,并通过像上述的参数对象(var generateData = require("../scripts/generateData.js")(parameters);
)的其他建议,请做一个机智的回应!
其他问题:我在做什么正确的,我做错了什么,什么和在哪里可以阅读,以更好地理解这些概念?我看了Mozilla Dev和其他一些网站。我发现有关自动执行匿名函数的参数,但没有一个从外部文件调用。也许我甚至没有寻找正确的东西...
后来编辑: 我已经尝试导出函数并从另一个文件调用它。这是简单的方法。
scriptThatRequiresTheExportedModuleAndCallsItWithParameters(参数).js文件
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我认为这意味着*立即* - 调用函数表达式。 – 2016-07-25 19:01:08
@torazaburo:我听说过这两种,但是你的是我喜欢和想要使用的。 :-) –