2014-07-27 58 views
2

這裏內返回一個函數是我用實驗的代碼示例:一個功能

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

hello(); 

這裏是一個repl.it鏈接

我想從我的函數bye返回函數hi。正如你所看到的,當我console.log(hi)值出現,但我的返回語句不會返回hi函數。爲什麼return聲明不會將參考文獻返回到hi

+8

'回報再見();' – elclanrs

+0

再見( )返回hi()它有任何意義嗎? – Dalorzo

回答

1

不要讓想通過定義裏面另一個

功能的第一定義您hi功能這樣例如

function hi (message) 
{ 
    console.log(message) 
} 

它需要一個參數,並顯示在控制檯上的複雜

現在讓我們定義我們的bye函數

function bye() 
{ 
    hi(" Called from the function bye "); 
} 

沒有,當你調用bye,你在同一時間

bye(); // it will show on the console the message " Called from ... " 

hi如果你想從一個函數返回一個功能很容易,你像這樣定義

function hi (message) 
{ 
    console.log(message) 
} 
hi功能

bye函數返回hi這樣的函數

function bye() 
{ 
    return hi; 
} 

所有你現在需要做的,是調用bye功能,並給在控制檯中返回,應該顯示哪些參數,就這樣

bye()(" This is a sample message "); 
3

你忘了return再見。

return bye();