Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Scope passing Test&lt;/title&gt; &lt;script type="text/javascript"&gt; // 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 &lt;a id="testA"&gt;&lt;/a&gt; element var oAelement=document.getElementById("testA"); oAelement.addEventListener( "click" , (function(scopePass_My1,scopePass_My2) { return function() { a(scopePass_My1,scopePass_My2) } })(my1,my2) ,true); } &lt;/script&gt; &lt;/head&gt; &lt;body onload="yourSetupFunction()"&gt; &lt;a id="testA" href="#" onclick="return false;"&gt;Click to Test&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The actual magic is just a few lines</p> <pre><code>(function(scopePass_My1,scopePass_My2) { return function() { a(scopePass_My1,scopePass_My2) } })(my1,my2) </code></pre> <p>The first set of parens cause the contents to be evaluated to a function reference.</p> <p>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)</p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload