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.
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
sometestis held as a local variable that is inaccessible to functions outside its scope.If you want
sometestto accessclient, you must pass it, the following uses local variable: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.
e.g. using a closure:
Then call it as
foo.sometest3().Edit
Added call