对在java脚本中返回函数的函数感到困惑

QVS*_*VSJ 2 javascript

function a(){
   function b(){
      alert("hi");
   }
   return b();
}
function c(){
   var d = a();
}

c();
Run Code Online (Sandbox Code Playgroud)

当我执行上述操作时,我收到警报"hi".但如果我这样做

function a(){
    function b(){
       alert("hi");
    }
    return b();
}
function c(){
    var d = a();
    d();
}

c();
Run Code Online (Sandbox Code Playgroud)

我期待看到c();中的赋值和函数调用语句的两个警报,但我只得到一个警报.我究竟做错了什么?

Aru*_*hny 6

因为方法a没有返回任何东西,因为在return语句中a调用b但是b没有返回任何东西因此d是未定义的,这将导致d()抛出错误

演示:小提琴

function c() {
    var d = a();
    alert(d)
    d();
}
Run Code Online (Sandbox Code Playgroud)

如果您看到小提琴,则第二个警报显示未定义为d的值