admin管理员组文章数量:1391778
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.
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.
- +1 understanding function scope and global variables in JavaScript is really important. It is also a great starting point to learning about prototypal inheritance. – Stephano Commented May 17, 2010 at 18:41
8 Answers
Reset to default 3See exposing inner functions to the outer world.
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();
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.)
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
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
本文标签: JavaScriptHow do I call a function inside of a functionStack Overflow
版权声明:本文标题:JavaScript - How do I call a function inside of a function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744763793a2623910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论