方法返回undefined

时间:2017-10-13 13:00:48

标签: javascript

我在从void方法

中接收值时遇到问题

例如:

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

test2(data){
    return data;
}

我想收到test2的数据,但是我一直在说undefined我做错了什么?我怎样才能做到这一点?

这可能是如此基本但我只是想知道为什么我收到值undefined

2 个答案:

答案 0 :(得分:1)

在定义前添加function

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

function test2(data) {
    return data;
}

test();

答案 1 :(得分:0)

试试这样:

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

test2(data): void {
    return data
}