I have created a typescript class, and i have two functions inside it, which are supposed to compute the ratio of likes and dislikes.
export class MyClass {
thumbedUp?: number;
thumbedDown?: number;
likePercent?= () => Math.round((this.thumbedUp / (this.thumbedDown this.thumbedUp)) * 100);
dislikePercent?= () => Math.round((this.thumbedDown / (this.thumbedDown this.thumbedUp)) * 100);
}
but when i create a MyClass instance like myClass:MyClass = {thumbedUp: 10, thumbedDown: 12};
and i try to call myClass.likePercent, it returns undefined. I really don't understand why as my object is instanciated... Can you help me?
Thanks
CodePudding user response:
export class MyClass {
thumbedUp: number;
thumbedDown: number;
constructor(thumbedUp:number, thumbedDown:number ){
this.thumbedDown=thumbedUp;
this.thumbedUp=thumbedDown;
}
likePercent():number{
return Math.round((this.thumbedUp / (this.thumbedDown this.thumbedUp)) * 100);;
}
dislikePercent():number{
return Math.round((this.thumbedDown / (this.thumbedDown this.thumbedUp)) * 100);
}
}
var myclass = new MyClass(10,12);
console.log(myclass.likePercent())
CodePudding user response:
do like me
export class MyClass {
thumbedUp: number;
thumbedDown: number;
constructor(){
this.thumbedDown=0;
this.thumbedUp=0;
}
likePercent():number{
return Math.round((this.thumbedUp / (this.thumbedDown this.thumbedUp)) * 100);;
}
dislikePercent():number{
return Math.round((this.thumbedDown / (this.thumbedDown this.thumbedUp)) * 100);
}
}