Javascript 为什么在对象返回值时返回undefined
试图理解在安慰输出时返回“undefined”的原因Javascript 为什么在对象返回值时返回undefined,javascript,Javascript,试图理解在安慰输出时返回“undefined”的原因 var learnFn = (function(){ var callMe = function(){ console.log('hi'); } return { name:"tom", callMe: callMe } })(); console.log(learnFn.callMe()); Output: "hi" undefined 您正在调用的
var learnFn = (function(){
var callMe = function(){
console.log('hi');
}
return {
name:"tom",
callMe: callMe
}
})();
console.log(learnFn.callMe());
Output:
"hi"
undefined
您正在调用的函数:
…没有返回语句。因此,它返回
undefined
(然后在该函数中的console.log
语句运行后记录该函数)。正在调用的函数:
…没有返回语句。因此,它返回
undefined
(然后在该函数中的console.log
语句运行后记录它)。因为cosole.log()
返回undefined
。如果你在开发工具中运行这个,你会看到确切的情况。@vlaz:这不是原因。在上面的代码中,console.log()
返回什么并不重要。@MichaelGeary true,我没有太注意函数是什么-我以为有人在控制台中执行一些代码,并对来自console.log
的未定义的感到困惑。经常发生足以愚弄我的事情。@vlaz是的,我对console.log中未定义的内容感到困惑。现在明白了,但如何摆脱它?@godfreyfernandes不会记录返回undefined
的函数调用,或者,也不要从记录结果的函数调用中返回undefined
。因为cosole.log()
返回undefined
。如果你在开发工具中运行这个,你会看到确切的情况。@vlaz:这不是原因。在上面的代码中,console.log()
返回什么并不重要。@MichaelGeary true,我没有太注意函数是什么-我以为有人在控制台中执行一些代码,并对来自console.log
的未定义的感到困惑。经常发生足以愚弄我的事情。@vlaz是的,我对console.log中未定义的内容感到困惑。现在明白了,但如何摆脱它?@godfreyfernandes不会记录返回undefined
的函数调用,或者,也不会从记录结果的函数调用中返回undefined
。
var callMe = function(){
console.log('hi');
}