Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数访问中的Javascript函数_Javascript_Function_Object - Fatal编程技术网

函数访问中的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