函数访问中的Javascript函数
如何访问函数中的函数?下面是一个例子:函数访问中的Javascript函数,javascript,function,object,Javascript,Function,Object,如何访问函数中的函数?下面是一个例子: 函数fun1(){ //这里有一些代码 变量示例=函数(){ //这里有一些代码 } 返回{ 例句:例句 } } 当我试图像这样访问它时,fun1.example()我得到错误fun1.example。example不是一个函数。我做错了什么?您必须将代码作为fun().example()运行,或者将fun()的结果存储到一个变量中,然后运行它,如下所示: var fun = (); fun.example(); 请查看评论了解更多信息 您需要调用外部
函数fun1(){
//这里有一些代码
变量示例=函数(){
//这里有一些代码
}
返回{
例句:例句
}
}
当我试图像这样访问它时,
fun1.example()
我得到错误fun1.example。example不是一个函数。我做错了什么?您必须将代码作为fun().example()运行,或者将fun()的结果存储到一个变量中,然后运行它,如下所示:
var fun = ();
fun.example();
请查看评论了解更多信息
您需要调用外部函数来获取返回值:
fun1().example()
(并修复语法错误)。发布的代码在语法上不正确。
function fun1() {
//some code here
var example = function () {
//some code here
};
return {
example: example,
};
}
// Fun1 is funtion, In funtion there is no property like example
// So when you call fun1.example , which undefined. Is not a function
fun1.example() // Error here
const obj = fun1() // call funtion to get obj
obj.example() // No error