1

I have Created a Class Circle. Here

  1. _radius is a private parameter
  2. _areaCalculate is a private method

After Calculate the value from private method _areaCalculate. I need this value to public method areaPrint. But it show me undefined.

const _radius = new WeakMap()
const _areaCalculate = new WeakMap()

class Circle {
    constructor(r) {
        _radius.set(this, r)
    }
    [_areaCalculate]() {
        return (Math.PI * Math.pow(this.radius, 2)).toFixed(2)
    }

    areaPrint() {
        console.log("The area of Circle is: " + _areaCalculate.get(this))
    }
}
let c = new Circle(4)
c.areaPrint()
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
mrhrifat
  • 65
  • 1
  • 7
  • 2
    `[_areaCalculate]()` does not create a method called `_areaCalculate`, it creates a method called `"[object WeakMap]"` since the `_areaCalculate` would be converted to a string. – VLAZ Sep 19 '21 at 08:13
  • With that said, none of your code puts anything in the `_areaCalculate` WeakMap. – VLAZ Sep 19 '21 at 08:14
  • Then, How to make _areaCalculate() a private method and get value from this. @VLAZ – mrhrifat Sep 19 '21 at 08:14
  • How are `_radius` and `_areaCalculate` considered as "privates", they're just variables in the outer scope of the class. If all these are in a function, then you've an antipattern at hands. – Teemu Sep 19 '21 at 08:15
  • [Private properties in JavaScript ES6 classes](https://stackoverflow.com/q/22156326) – VLAZ Sep 19 '21 at 08:16

1 Answers1

1

If one sticks with the OP's approach of utilizing a weak map for accessing a Circle instance' "private member" through a prototypal method, then one just needs to simplify the code to a single reference map and a function which calculates a circle instance' area on the fly ...

function getComputedArea(circle) {
  return (Math.PI * Math.pow(rMap.get(circle), 2)).toFixed(2);  
}
const rMap = new WeakMap();

class Circle {
  constructor(radius) {
    rMap.set(this, radius);
  }
  areaPrint() {
    console.log(
      `A radius ${ rMap.get(this) } circle area is ${ getComputedArea(this) }`
    );
  }
}
let a = new Circle(4);
let b = new Circle(9);

a.areaPrint();
b.areaPrint();

... or one follows VLAZ's advice and starts utilizing the private field declaration syntax for private instance fields.

Edit

From the further beneath comment-based discussion with Bergi ...

"Private methods, unlike private fields, are allocated on the prototype not on the instance, just the like their respective public counterparts" . – Bergi

... the implementation for getComputedArea changed from a local helper function to a private instance method.

class Circle {

  #getComputedArea(radius) {
    return (Math.PI * Math.pow(this.#radius, 2)).toFixed(2);
  }
  #radius;

  constructor(radius) {
    this.#radius = radius;
  }
  areaPrint() {
    console.log(
      `A radius ${ this.#radius } circle area is ${ this.#getComputedArea() }`
    );
  }
}
let a = new Circle(4);
let b = new Circle(9);

a.areaPrint();
b.areaPrint();
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • In the second version, you'd also make `getComputedArea` a private method – Bergi Sep 19 '21 at 10:20
  • @Bergi ... It already is a local function within a module scope. Why should one add complexity around something that is just a utility/helper function? Making it a private method, will add another weak map handling (or something similar) behind the scene. – Peter Seliger Sep 19 '21 at 10:24
  • 1
    For consistency and the ability to access `this.#radius` in the method itself instead of requiring a parameter. I think it would simplify the code, not add complexity. It's what the OP asked for (even if personally I would recommend to make it a public method). I doubt the implementation characteristics in current engines should affect that decision. – Bergi Sep 19 '21 at 10:32
  • @Bergi ... I have to think about this argument because if it comes to types and/or type systems I always encourage people to keep the footprint low, for both the implementation itself (e.g. leanest possible implementation of constructors/factories with a lot of forwarding to helper functionality) and for the handling of resources which have to be allocated with every instance. – Peter Seliger Sep 19 '21 at 10:37
  • 1
    Private methods, unlike private fields, are allocated on the prototype not on the instance, just the like their respective public counterparts. – Bergi Sep 19 '21 at 10:40
  • 1
    @Bergi ... Good point. I wasn't aware of the latter. Thanks. I changed the 2nd example code accordingly. – Peter Seliger Sep 19 '21 at 11:01