简体   繁体   English

我将一个函数(本身具有一个参数)作为参数传递给另一个函数,以及如何获取该参数

[英]I passed a function(which itself has a parameter) as a parameter into another function and how to get the parameter

I passed a function(which itself has a parameter let's say para) as a parameter into another function function b() and how to get the parameter para. 我将一个函数(其本身具有一个参数,比如说para)作为参数传递给另一个函数b()以及如何获取参数para。

here is the simple example 这是简单的例子

<input type="button" onclick="sometest3()" value="Run test">

<script>
function sometest3() {

    // pass an anonymous function as a parameter which has
    // its own parameter "client"
    sometest('connection',function(client){client.getInfo();}) 
}


function sometest(eve,func) {

     // get this function's parameter which is "client" and pass
     // a reference of sometest2 to it. so in the callback I can use
     client.getInfo(); 
}


function sometest2() {
   this.getInfo=function (){alert("get it");};
}
</script>

You can't given the code you've posted. 您无法提供已发布的代码。 The anonymous function passed to sometest is held as a local variable that is inaccessible to functions outside its scope. 传递给sometest的匿名函数被保存为本地变量,该变量在其范围之外是不可访问的。

If you want sometest to access client , you must pass it, the following uses local variable: 如果要通过sometest访问client ,则必须将其传递,以下使用局部变量:

function sometest3() {

    var client = {getInfo: function(){ /* whatever */}};

    sometest('connection', function(client){client.getInfo();}, client);
}


function sometest(eve, func, client) {

    // The client object has been passed from sometest3
    func(client);

}

Or you could make it available to both functions in a closure or global variable, or property of some object accessable to both functions (essentially these same thing). 或者,您可以使它对于闭包或全局变量中的两个函数均可用,或者使某些对象的属性可以访问这两个函数(本质上是同一件事)。 Your choice. 你的选择。

eg using a closure: 例如使用闭包:

var foo = (function() {
    var client = {getInfo: function(){ /* whatever */}};
    return {
        sometest3: function() {
            foo.sometest('connection', function(obj){obj.getInfo();}, client);
        },

        sometest: function(eve, func, client) {
            // The client object has been passed from sometest3
            func(client);
        }
    };
}());

Then call it as foo.sometest3() . 然后将其称为foo.sometest3()

Edit 编辑

Added call 新增通话

If you want to know how many arguments are named in a function func you can check func.length . 如果您想知道函数func中命名了多少个参数,可以检查func.length And this doesn't mean that you can't pass more or less arguments to that function, of course, but only how many of them are stored in local scoped variables. 当然,这并不意味着您不能将更多或更少的参数传递给该函数,而只能将其中的参数存储在局部范围的变量中。

But if you want to know the names of those variables the best thing to do is to convert the function to a string and get its declaration: 但是,如果您想知道这些变量的名称,最好的办法是将函数转换为字符串并获取其声明:

var parms = String(func)
        .match(/function[^\(]*\(([^\)]*)/)[1]
        .split(/\s*,\s*/);

(Mind you that putting comments between the function keyword and its declaration may mess up this method.) (请注意,在function关键字及其声明之间添加注释可能会使该方法弄乱。)

That being said, I fail to see how this can be useful for you... 话虽如此,我看不出这对您有什么帮助...

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

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