This example uses the assignment of an onclick event handler
during page load to demonstrate how to create a function
reference containing embedded references to then local
values in the setup function.
<html>
<head>
<title>Scope passing Test</title>
<script type="text/javascript">
// Your target function
function a(p1,p2) { alert("p1="+p1+ " p2="+p2); }
function yourSetupFunction()
{
// Declare some local variables that your "a()" function cannot see to test.
var my1 ="Look Ma" ;
var my2 ="No hands" ;
// Snag a reference to the <a id="testA"></a> element
var oAelement=document.getElementById("testA");
oAelement.addEventListener( "click" ,
(function(scopePass_My1,scopePass_My2)
{
return function()
{
a(scopePass_My1,scopePass_My2)
}
})(my1,my2) ,true);
}
</script>
</head>
<body onload="yourSetupFunction()">
<a id="testA" href="#" onclick="return false;">Click to Test</a>
</body>
</html>
The actual magic is just a few lines
(function(scopePass_My1,scopePass_My2)
{
return function()
{
a(scopePass_My1,scopePass_My2)
}
})(my1,my2)
The first set of parens cause the contents to be evaluated to a function reference.
The second set of parens ... (my1,my2) ... cause the function reference to be called
which returns another function reference which can see the parameters passed in
... scopePass_My1,scopePass_My2 ... and is able to pass these to your target function a(p1,p2)
Hope that helps.