Reputation: 51
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.
Upvotes: 5
Views: 355
Reputation: 31
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.
Upvotes: 0
Reputation: 5866
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.
Upvotes: 1
Reputation: 344571
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
Upvotes: 2
Reputation: 28220
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.)
Upvotes: 2
Reputation: 17977
function parentFunc() {
var childFunc = function() {
...
}
...
}
childFunc(); // works
Upvotes: -1
Reputation: 86902
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();
Upvotes: 2
Reputation:
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.
Upvotes: 0