【问题标题】:Method return undefined [duplicate]方法返回未定义[重复]
【发布时间】:2018-03-25 15:04:49
【问题描述】:

我在从 void 方法接收值时遇到问题

例如:

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

test2(data){
    return data;
}

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

这可能很基础,但我只是想知道为什么我会收到 undefined 的值

【问题讨论】:

  • 有什么错误吗?或者日志是undefined?
  • 该代码看起来不错。
  • 日志未定义
  • 您还应该在函数名称前包含单词function。 function test() { 和 function test2() {

标签: javascript


【解决方案1】:

试试这样:

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

test2(data): void {
    return data
}

【讨论】:

  • 这不是 JavaScript。
【解决方案2】:

在定义前添加function。

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

function test2(data) {
    return data;
}

test();

【讨论】:

  • ……然后删除this.,或者什么?
  • 这对我有用
猜你喜欢
  • 2017-07-06
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
  • 2019-10-01
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多