[英]javascript function within the function
So I was wondering if it is possible to access a variable (which has a value of function) from outside the scope.所以我想知道是否可以从 scope 外部访问变量(具有函数值)。 I have code that goes something like this:我有这样的代码:
function parentFunction(){
var childFunction = function() {
// do something
}
}
$(function(){
// need to access childFunction() here.
});
var childFunction;
function parentFunction(){
childFunction = function() {
// do something
}
}
$(function(){
childFunction();
});
No, you do not.你不可以。 The only way to achieve this is to make the desired childFunction an attribute of parentFunction:实现这一点的唯一方法是使所需的 childFunction 成为 parentFunction 的属性:
var parentFunction = (function(){
var actualParentFunction = function(){
this.childFunction = function() {
// do something
};
}
return new actualParentFunction();
})();
At which point you can do:此时您可以执行以下操作:
parentFunction.childFunction();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.