1

I've created a class in Typescript. When I am trying to convert this into js and run using command prompt, I am not getting desired output. Here is my class

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) .

Can u suggest what's not working in this.?

2
  • 2
    myobj.value() parenthesis missing ? Commented Jul 17, 2020 at 12:53
  • 1
    this is typio. u missed parenthesis Commented Jul 17, 2020 at 12:54

1 Answer 1

2

myobj.value is a function.

To print the actual value you have to call the function using myobj.value()


You can spot your mistake using the typeof keyword, that tells you the type of a variable.

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.