2011-12-13 245 views
2

我在对的NodeJS下面的函数,res.execSync发生在多个参数如下详细介绍: https://github.com/xdenser/node-firebird-libfbclient如何将参数传递给函数?

function execSync(param1, param2, ..., paramN);
param1, param2, ..., paramN - parameters of prepared statement in the same order as in SQL and with appropriate types.

function test(sql, callback) 
{ 
    var args = Array.prototype.slice.call(arguments).splice(2); 
    res.execSync(args); 
} 

test('test', function() {}, "param1", "param2", "param3"); 

错误:期待字符串作为参数1#。

我该如何解决这个问题?

回答

1

难道你的意思是:

function test(sql, callback) 
{ 
    var args = Array.prototype.slice.call(arguments, 2); 
    res.execSync.apply(res, args); 
} 

test('test', function() {}, "param1", "param2", "param3"); 
1

args是一个数组。您需要使用apply方法将其解包为单独的参数。

res.execSync.apply(res, args); 

它的工作原理就像call但接收阵列这一翻译一般的参数列表中。


顺便说一句,您可以传递范围参数切片。这意味着是写你的第一线较短的方式:

var args = Array.prototype.slice.call(arguments, 2);