在 NodeJS 中将多个函数参数合并为一个



是否可以在nodejs中将多个函数参数/参数组合成一个?例如:

var foo = “test1”
var bar = “test2”
var combined = foo,bar
function myFunction(input){
console.log(foo)
console.log(bar)
}
myFunction(combined)
//test1
//test2

编辑:我正在尝试将其与shelljs-exec-proxy一起使用:

var shell = require('shelljs-exec-proxy')
var currentSourceLine = "/home/haveagitgat/Desktop/1/1'.mp4"
var currentDestinationLine = "/home/haveagitgat/Desktop/2/1'.mp4"

//Normally called like this 
shell.HandBrakeCLI('-i', currentSourceLine, '-o', currentDestinationLine, '-Z', 'Very Fast 1080p30');
//Would like to call with something like this
var combined = '-i', currentSourceLine, '-o', currentDestinationLine, '-Z', 'Very Fast 1080p30'

shell.HandBrakeCLI(combined);

一个简单的解决方案是使用一个对象,例如:

var foo = "test1"
var bar = "test2"
var combined = {
   foo,
   bar
}
function myFunction(input){
console.log(input.foo)
console.log(input.bar)
}
myFunction(combined)
编辑

:您编辑的问题的解决方案

var combined = ['-i', currentSourceLine, '-o', currentDestinationLine, '-Z', 'Very Fast 1080p30'] // This is an Array of arguments!
shell.HandBrakeCLI(...combined); // This uses argument destructuring

相关内容

最新更新