为什么我的函数返回“未定义”

时间:2019-07-19 05:57:20

标签: javascript class object static undefined

我想从Check类获得一个值,但总是得到undefined

class Check {
 static testget() {
    let tester = 1;
    return tester;
  }
}

window.onscroll = function() {
  const test = new Check();
  console.log(test.testget);
};

它应该在控制台上显示1,但我总是得到undefined

1 个答案:

答案 0 :(得分:1)

我会要求您花一些时间在Classes: Static methods上。

由于成员( testget )是 static ,因此您无法使用该类的实例( test )访问该成员,请使用< em> class 名称。另外,在函数名称后指定()来调用该函数:

console.log(Check.testget());