2014-01-08 24 views
1

夥計們, 試圖找出這兩個語句之間的差異,爲什麼一個作品,另將引發一個錯誤:CoffeeScript中沒有通過可變

作品:

jsonFileContents = fs.readFileSync('sample.json', 'utf8') 
res.send(jsonFileContents) 

不會:

jsonFileContents = fs.readFileSync('sample.json', 'utf8') 
returnResult(jsonFileContents) 

returnResult = (data) -> 
    res.send data 

錯誤拋出:

`TypeError: undefined is not a function` 
+0

人,我充分認識到這一功能的是同步的:)這裏只是測試的東西,並見狀,認爲我會問:)謝謝! – Cmag

回答

1

這是因爲returnResult沒有在使用時定義。

下面應該工作:

returnResult = (data) -> 
    res.send data 

jsonFileContents = fs.readFileSync('sample.json', 'utf8') 
returnResult(jsonFileContents) 
+0

所以通過放置returnResult上面的調用修復了這個? – Cmag

+0

@Cmag是的,因爲然後在使用它之前定義了returnResult。 – aknuds1