Advertisement
Guest User

Untitled

a guest
Dec 26th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // You don't need the constructor if you use initializers on the fields:
  2. class SW {
  3.     private startTime: number = 0;
  4.     // −−−−−−−−−−−−−−−−−−−−−−^^^^
  5.     private endTime: number = 0;
  6.     // −−−−−−−−−−−−−−−−−−−−^^^^
  7.  
  8.     start() {
  9.         this.startTime = Date.now();    
  10.     }
  11.  
  12.     stop() {
  13.         this.endTime = Date.now();  
  14.     }
  15.  
  16.     getDuration() {
  17.         const seconds = (this.endTime - this.startTime) / 1000;
  18.     }
  19. }
  20.  
  21. // In fact, if you do that, you don't even need to write the type explicitly
  22. // because TypeScript will infer `number` from the initializer
  23. class SW {
  24.     private startTime = 0;
  25.     // −−−−−−−−−−−−−−^^^^
  26.     private endTime = 0;
  27.     // −−−−−−−−−−−−^^^^
  28.  
  29.     start() {
  30.         this.startTime = Date.now();    
  31.     }
  32.  
  33.     stop() {
  34.         this.endTime = Date.now();  
  35.     }
  36.  
  37.     getDuration() {
  38.         const seconds = (this.endTime - this.startTime) / 1000;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement