提问者:小点点

函数值不以角度显示


我用打字稿创建了一个类。 当我试图将其转换为js并使用命令提示符运行时,我没有得到所需的输出。 这是我的课

class myclass{
    
    j: number;

    constructor(k: number){
        this.j = k;

    }
    value (){
        return("the number is "+this.j)
    }

}

let myobj = new myclass(10)
    console.log(myobj.value) .

你能建议什么是不起作用的。?


共1个答案

匿名用户

MyObj.value是一个函数。

要打印实际值,必须使用MyObj.value()调用函数

您可以使用typeof关键字来发现错误,该关键字告诉您变量的类型。

null

class myclass {
  j;

  constructor(k) {
    this.j = k;
  }
  
  value() {
    return `the number is ${this.j}`;
  }
}

const myobj = new myclass(10);

console.log(typeof myobj.value);