kstoyanov

03. HEX

Oct 6th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Hex {
  2.   constructor(value) {
  3.     this.value = Number(value);
  4.   }
  5.  
  6.   valueOf() {
  7.     return this.value;
  8.   }
  9.  
  10.   toString() {
  11.     return `0x${this.value.toString(16).toUpperCase()}`;
  12.   }
  13.  
  14.   plus(arg) {
  15.     if (arg instanceof Hex) {
  16.       return new Hex(this.value + arg.valueOf());
  17.     }
  18.     return new Hex(this.value + Number(arg));
  19.   }
  20.  
  21.   minus(arg) {
  22.     if (arg instanceof Hex) {
  23.       return new Hex(this.value - arg.valueOf());
  24.     }
  25.     return new Hex(this.value - Number(arg));
  26.   }
  27.  
  28.   parse(hex) {
  29.     return parseInt(`${hex}`, 16);
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment