2017-10-13 59 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