overriding valueOf and toString not working as expected

84 Views Asked by At

I am trying to override valueOf and toString

This is becouse I want to add some properties to the premitive values

So I created this below

class Box {
  #value;
  constructor(value) {
    this.#value = value;
  } 
  
  valueOf() {
    return this.#value;
  }
  
  toString(){
    return this.#value;
  }
  
  someMethod(){
    return "something else"
  }
}

const t1 =21;
const t2 = 21
const p = new  Box(45);
const s = new  Box(45);
console.log(p === s, t1 === t2)

notice that t1 and t2 comparesion works great but p and s are not working as expected

Why is that and is there a way to do the thing I want?

0

There are 0 best solutions below