Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Javascript Help: reference a function within a function?

Is there a way to reference a function that is within another function.

I want to do something like

<input onclick="return One.Two();" />

Basically calling the Two function. But I won't to keep it within

the One function.

function One()

{

............

function Two()

{

.........

}

}

Thanks!!

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    If your Two() finction is declared using the "this" keyword, it can be dot-referenced in the namespace of One(). Defining Two() as you have in your example, the function is private, and can't be referenced from outside. Here's an example to clarify the issues.

    <html>

    <head>

    <script type="text/javascript">

    function object1() {

    // this function is private and cannot be

    // called directly from outside the object

    function private(msg, eid) {

    // write {msg} to container with id {eid}

    var d = document;

    var el = d.getElementById(eid);

    el.innerHTML = msg;

    }

    // this function is public and can be

    // called from outside of the object,

    // using a dotted namespace reference

    this.privileged = function(msg, eid) {

    // from here, the private() function is

    // visible and can be called

    private(msg, eid);

    }

    }

    function showErr(e) {

    alert(e.message);

    }

    // create an instance for use in the page

    var o = new object1();

    </script>

    <head>

    <body>

    <form>

    Direct call to private() will fail...<br />

    <input type="button"

    value="click to call private()"

    onclick="{ try { o.private( 'private() called', 'msg'); } catch (e) { showErr(e); }}" />

    <br />

    Indirect call to private() mediated by privileged() will work...<br />

    <input type="button"

    value="click to call privileged()"

    onclick="o.privileged( 'privileged() called', 'msg');" />

    <br />

    </form>

    <div id="msg"></div>

    </body>

    </html>

  • Erika
    Lv 4
    5 years ago

    once you define your variables in worldwide scope (no longer interior any function), they are already worldwide variables. occasion: var globalVar1 = 5; var globalVar2 = "blah"; myFunc1(); function myFunc1 () { alert (globalVar1); // this might alert: 5 alert (globalVar2); // this might alert: "blah" globalVar1 = 10; globalVar2 = "try"; myFunc2(); } function myFunc2 () { alert (globalVar1); // this might alert: 10 alert (globalVar2); // this might alert: "try" } ------------------ reaction on your extra information: Please study the entire answer :) Or probably study your guy or woman question returned. There are some factors that are cut back off (this web site annoyingly cuts off long words), in case the lacking factors replaced what your question seems to be asking now. on your occasion, you look to think of which you will desire to reassign the values of the worldwide variables interior the function Resize(), jointly as they already have their values assigned while they are defined in the worldwide scope. So what i'm asserting is, when you have this: var Site_height = checklist.documentElement.clien... var Site_width = checklist.documentElement.clien... var Box1_height = Site_height*0.5; var Box1_width = Site_width*0.5; the values are waiting, and you are able to purely use it in any function, without desiring to reassign the values returned. e.g.: you are able to purely have: function Resize () { checklist. getElementById ("box1"). type. height = Box1_height + "px"; checklist. getElementById ("box1"). type. width = Box1_width + "px"; }

  • Anonymous
    1 decade ago

    oola

Still have questions? Get your answers by asking now.