2017-10-13 99 views
0

我有一個問題,從一個void方法方法返回undefined

接收的值。例如:

test() { 
    var x = this.test2("hi there"); 
    console.log(x); 
} 

test2(data){ 
    return data; 
} 

我希望收到來自test2的數據,但它一直在說undefined我該怎麼辦錯這裏?我該如何做這項工作?

這可能是如此基本的,但我只是想知道爲什麼我得到的價值undefined

+0

任何錯誤?或者日誌是'undefined'? –

+0

該代碼看起來很好。 – Cerbrus

+0

日誌未定義 – Bcoded

回答

0

中定義的前面添加function

function test() { 
 
    var x = this.test2("hi there"); 
 
    console.log(x); 
 
} 
 

 
function test2(data) { 
 
    return data; 
 
} 
 

 
test();

+0

...並刪除'this.',或者什麼? – Bergi

+0

這爲我工作 – Bcoded

0

嘗試這樣的:

test(): void { 
    var x = this.test2("hi there") 
    console.log(x); 
} 

test2(data): void { 
    return data 
} 
+0

這不是JavaScript。 – Bergi