简体   繁体   中英

JavaScript - How do I call a function inside of a function?

I have the following JavaScript code:

function parentFunc() {

    function childFunc() {
        ...
    }

    ...
}

// outside of myParentFunc, how do I call myChildFunc() ?
childFunc(); // doesn't work

How do I call childFunc() from outside of parentFunc() ?

UPDATE :

I know the obvious answer would be to move childFun outside of parentFun , but this is a third party library that I can't modify.

请参阅将内部函数暴露给外部世界。

This would be one way you could do it.

var myChildFunction;

function myParentFunc() {
  //....
  myChildFunction = function () {
    //....
  }
  myChildFunction();
}

myChildFunction();

Here is another way

var myChildFunction = function () {
  //....    
}


function myParentFunc() {
  //....
  myChildFunction();
}

myChildFunction();

You may want to consider using the Yahoo Module Pattern instead:

myModule = function () {

    //"private" variables:
    var myPrivateVar = "I can be accessed only from within myModule."

    //"private" methods:
    var myPrivateMethod = function () {
        console.log("I can be accessed only from within myModule");
    }

    return {
        myPublicProperty: "I'm accessible as myModule.myPublicProperty."

        myPublicMethod: function () {
            console.log("I'm accessible as myModule.myPublicMethod.");

            //Within myModule, I can access "private" vars and methods:
            console.log(myPrivateVar);
            console.log(myPrivateMethod());
        }
    };
}();

You define your private members where myPrivateVar and myPrivateMethod are defined, and your public members where myPublicProperty and myPublicMethod are defined.

You can simply access the public methods and properties as follows:

myModule.myPublicMethod();    // Works
myModule.myPublicProperty;    // Works
myModule.myPrivateMethod();   // Doesn't work - private
myModule.myPrivateVar;        // Doesn't work - private

If you absolutely cannot modify the code, there is no way to do it. Otherwise assign the inner function to something you can access, by changing the line

function childFunc() {

to, say,

window.cf = childFunc() {

then you can access the function through the cf variable. (There are many other ways to do it; this is the one that requires the least change in the third-party code.)

There is a way to do this very simply, but it is also a very bad practice to get into. However, because OCCATIONALLY global variables are handy, I'll mention this. Please note that in the situation you are describing is not a good example of using global variables.

The following code will work but is horrible code .

function parentFunction() {
    ...
    childFunction = function() {
    ...
    }
}

childFunction();

You are making childFunction global, which is generally a horrible idea. Using namespaces is a way to get around the global variable insanity.

ABV = {};
ABV.childFunction = function() {
    ...
}
ABV.parentFunction = function() {
    ...
    ABV.childFunction();
    ...
}
ABV.childFunction();

This is how libraries like DWR and such work. they use 1 global variable and put all their children inside that one global variable.

Thinking about how scope works in JavaScript is really important. If you start throwing global variables around you are bound to run into lots of trouble. From the example you are using, it is clear that you need something like a class of "functions" that you can call from anywhere. Hope that helps.

Just declare it outside the function o_O

Well, if the library doesn't provide a way to do it, modify it and upload it with your .js, probably the library doesn't allow you to do it because you shouldn't.

Here is what you can do:

attach the inner function to global:

function parentFunc() {

   window.childFunc = function(){

   }

}

This is kind of ghetto though :)

So I would use some of the more sophisticated OO patterns, such as the module patter:

function parentFunc() {

   var someVar;

   var childFunc = function(){

   };


   return {
      childFunc,
      someVar
   }

}

parentFunc.childFunc();

This way you can return your inner function and your return value.

Remember, the fact that you cannot call your inner function, is a feature of JS not a limitation.

function parentFunc() {

    var childFunc = function() {
        ...
    }

    ...
}

childFunc(); // works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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