如何在js中调用另一个函数表达式中定义的函数? [英] How do I call a function defined with in a another function expression in js?

查看:82
本文介绍了如何在js中调用另一个函数表达式中定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法调用另一个中定义的函数



我尝试过:



var func1 =(function(){

function func2(){

console.log(wrkng);

};

})();

如何调用func2()???

func1.func2();给出错误说func1未定义..

could not call a function that is defined in another

What I have tried:

var func1 = (function() {
function func2() {
console.log("wrkng");
};
})();
how can i call func2()???
func1.func2(); gives error saying func1 is undefined..

推荐答案

不能!由于它是私人功能,请使用模块模式 [ ^ ]将其暴露在外面

You Cannot! since it is a private function, use Module pattern[^] to expose it outside as
var func1 = (function () {
           func2 =  function() {
                console.log("wrkng");
           };
           return {func2:func2}

        })();







func1.func2()


AFAIK,你不能。内部函数的可见性范围仅限于其定义的函数。
AFAIK, you can't. The visibility scope of the inner function is limited to the function it is defined in.


这篇关于如何在js中调用另一个函数表达式中定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆