简体   繁体   English

JavaScript:获取传递函数的参数?

[英]JavaScript: Get parameters of passed function?

As the title says I'm wondering if it's possible to get the parameters of a passed function. 如标题所述,我想知道是否有可能获取传递函数的参数。 After hours of searching and looking at similar questions I'm still no closer, so I'll attach a simple example rather then what I'm working on - as I'm starting to suspect it's not possible. 经过数小时的搜索并查看了类似的问题,我仍然走得很近,所以我将附上一个简单的示例,而不是我正在研究的内容-因为我开始怀疑这是不可能的。

Intended global function 预期的全局功能

function getTransaction(anyMethod)
{   
    db.transaction
    (
        function(transaction) 
        {
            anyMethod(transaction);         
        },

        function errorCB(err) {
            redirectToLoginWithError("Error processing SQL");
        },

        function successCB() {
            ;//alert("Success!");
        }

    );  
}

Functions to be called 要调用的功能

function iWork(tx)
{
  tx.doSomething();
}

function iDontWork(tx, param1, param2)
{
  tx.doSomething(param1, param2);
}

Actual call 实际通话

// Works fine
getTransaction(iWork);
// The problem
getTransaction(iDontWork, value1, value2);

getTransaction(iDontWork2, value1, value2, ..., valueX);

I've tried several different approaches, but none have proved successful so far. 我尝试了几种不同的方法,但是到目前为止,没有一种方法被证明是成功的。 The closest (although not very) have been 最接近(尽管不是很接近)的是

getTransaction(function(){iDontWork(value1, value2)})); getTransaction(function(){iDontWork(value1,value2)}));

This does call the correct function via the getTransaction, but does not pass the parameters correctly: Params (value1, value2) are kept, but the transaction object is lost / undefined. 这确实通过getTransaction调用了正确的函数,但没有正确传递参数:保留了参数(值1,值2),但是事务对象丢失/未定义。 I can see why this does happen, but I cannot see any solution to it. 我知道为什么会发生这种情况,但是我看不到任何解决方案。 All said, I'm also open to that the getTransaction should be scrapped and re-written somehow. 所有人都说过,我也开放应该废弃getTransaction并以某种方式对其进行重写。 The point is to get a flexible method that scales well. 关键是要获得一种可扩展的灵活方法。

Simply refactor getTransation to take a function and an array of arguments. 只需重构getTransation就可以接受一个函数和一个参数数组。 Prepend the transaction variable to the argument array and call the function using apply : transaction变量放在参数数组之前,然后使用apply调用函数:

function getTransaction(anyMethod, methodArgs) {
    var someFunction = anyMethod;
    // if no args provided, use an empty array
    methodArgs = methodArgs || [];

    db.transaction (
        function(transaction) {
            // transaction is always the first arg; prepend it to the arg list
            methodArgs.unshift(transaction);
            // call method with argument array
            anyMethod.apply(null, methodArgs);
        },

    // ...

    );
}

Simply use it with: 只需将其与:

doTransaction(iDontWork, [param1, param2]);
doTransaction(iWork);

EDIT : 编辑

As @rambo coder pointed out, you could just use regular arguments (instead of an array of arguments) by slicing arguments : 正如@rambo编码器指出的那样,您可以通过切片arguments来使用常规参数(而不是参数数组):

function getTransaction(anyMethod) {
    var someFunction = anyMethod;

    db.transaction (
        function(transaction) { 
            var methodArgs = arguments.slice(1);
            methodArgs.unshift(transaction);
            anyMethod.apply(null, methodArgs);
        },

     ...

This way lets you supply arguments directly to getTransaction , as you do in your example ( doTransaction(iDontWork, param1, param2); ), instead of putting them in an array. 这样,您就可以像在示例中一样将参数直接提供给getTransactiondoTransaction(iDontWork, param1, param2); ),而不是将它们放在数组中。

getTransaction(function (x) {
    iDontWork(x, value1, value2);
});

Your getTransaction assumes that its single argument is a function that takes one parameter. 您的getTransaction假定其单个参数是一个采用一个参数的函数。 You need to create a new function that takes one parameter, but also includes the specific parameter values you want (namely value1 and value2 ). 您需要创建一个新函数,该函数需要一个参数,但还要包含所需的特定参数值(即value1value2 )。


The above uses an anonymous function; 上面使用了匿名函数; you could also do it with a named function: 您也可以使用命名函数:

function forwarder(x) {
    iDontWork(x, value1, value2);
}

getTransaction(forwarder);

You can do it by creating partial functions: 您可以通过创建部分函数来做到这一点:

function iDontWork(param1, param2){
    return function(tx) {
        tx.doSomething(param1, param2);
    }
}

And: 和:

getTransaction(iDontWork(value1, value2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM