Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.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 - Fatal编程技术网

Javascript 返回函数中的函数

Javascript 返回函数中的函数,javascript,Javascript,下面是我正在试验的代码示例: var hello = function hi(){ function bye(){ console.log(hi); return hi; } bye(); }; hello(); 这里有一个链接 我试图从我的函数bye返回函数hi。如您所见,当Iconsole.log(hi)出现值时,但我的return语句不返回hi函数。为什么return语句没有返回对hi的引用?您忘记了return再见 return

下面是我正在试验的代码示例:

var hello = function hi(){
    function bye(){
        console.log(hi);
        return hi;
    }
    bye();
};

hello();
这里有一个链接


我试图从我的函数
bye
返回函数
hi
。如您所见,当I
console.log(hi)
出现值时,但我的return语句不返回hi函数。为什么
return
语句没有返回对
hi
的引用?

您忘记了
return
再见

return bye();

您忘了返回再见

return bye();

不要在另一个函数中定义一个函数,从而使思考变得复杂

首先定义
hi
函数,例如

function hi (message) 
{ 
     console.log(message) 
}
它接受一个参数并在控制台上显示它

现在让我们来定义我们的
bye
函数

function bye ()
{
     hi(" Called from the function bye ");
}
否当您呼叫
bye
时,您同时呼叫
hi

bye(); // it will show on the console the message " Called from ... " 
如果您想从函数返回函数,可以很容易地这样定义
hi
函数

function hi (message) 
{ 
     console.log(message) 
}
function bye() 
{
     return hi;
}
bye()(" This is a sample message "); 
bye
函数返回如下
hi
函数

function hi (message) 
{ 
     console.log(message) 
}
function bye() 
{
     return hi;
}
bye()(" This is a sample message "); 
现在需要做的就是调用
bye
函数,并在控制台中为返回的内容提供参数,如下所示

function hi (message) 
{ 
     console.log(message) 
}
function bye() 
{
     return hi;
}
bye()(" This is a sample message "); 

不要在另一个函数中定义一个函数,从而使思考变得复杂

首先定义
hi
函数,例如

function hi (message) 
{ 
     console.log(message) 
}
它接受一个参数并在控制台上显示它

现在让我们来定义我们的
bye
函数

function bye ()
{
     hi(" Called from the function bye ");
}
否当您呼叫
bye
时,您同时呼叫
hi

bye(); // it will show on the console the message " Called from ... " 
如果您想从函数返回函数,可以很容易地这样定义
hi
函数

function hi (message) 
{ 
     console.log(message) 
}
function bye() 
{
     return hi;
}
bye()(" This is a sample message "); 
bye
函数返回如下
hi
函数

function hi (message) 
{ 
     console.log(message) 
}
function bye() 
{
     return hi;
}
bye()(" This is a sample message "); 
现在需要做的就是调用
bye
函数,并在控制台中为返回的内容提供参数,如下所示

function hi (message) 
{ 
     console.log(message) 
}
function bye() 
{
     return hi;
}
bye()(" This is a sample message "); 
返回bye()
bye()返回hi()这有意义吗?
returnbye()
bye()返回hi(),这有什么意义吗?