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
。如您所见,当Iconsole.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(),这有什么意义吗?