簡體   English   中英

node.js module.exports返回未定義

[英]node.js module.exports returns with undefined

這是我的node.js

module.exports = function(){

   console.log('hello');

}

index.js

console.log(require('./node')());

產量

hello
undefined

為什么在函數調用后出現undefined ??

您的函數不返回任何內容,僅記錄到標准輸出。

真正發生的是:

console.log(require('./node')()); // our original code

console.log((function(){console.log("hello")})()); // function runs, prints "hello"

console.log(); // nothing is returned by the function, so it prints "undefined"

嘗試將功能更改為:

module.exports = function(){
   console.log('hello');

   return "HERE BE DRAGONS"
}

明白我的意思:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM