You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在JavaScript公共方法中获取私有方法的返回值

Fixing Undefined Output When Calling Private Method via WeakMap in JavaScript Circle Class

Let's walk through the issues in your code and get that area calculation working correctly:

What's Causing the undefined Output?

  1. You never stored the private method in your WeakMap: Your _areaCalculate WeakMap is empty for each Circle instance—you only set the _radius value in the constructor, never the method itself.
  2. Incorrect method definition syntax: Using [_areaCalculate]() as a class method doesn't bind that function to your WeakMap. _areaCalculate is a WeakMap instance, not a Symbol or valid property key, so this creates a weirdly named method that's never linked to your WeakMap.
  3. Accessing the radius incorrectly: this.radius doesn't exist—your radius is stored privately in the _radius WeakMap, so you need to retrieve it with _radius.get(this).

Corrected Code Option 1 (Inline Method in Constructor)

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

class Circle {
  constructor(r) {
    _radius.set(this, r);
    // Store the private calculation method in the WeakMap for this instance
    _areaCalculate.set(this, () => {
      // Retrieve the private radius from its WeakMap
      const radius = _radius.get(this);
      return (Math.PI * Math.pow(radius, 2)).toFixed(2);
    });
  }

  areaPrint() {
    // Get the method from the WeakMap and execute it
    const calculateArea = _areaCalculate.get(this);
    console.log("The area of Circle is: " + calculateArea());
  }
}

let c = new Circle(4);
c.areaPrint(); // Output: The area of Circle is: 50.27

Corrected Code Option 2 (Reusable External Function)

If you want to reuse the calculation logic across instances, define the function separately and bind it to the correct this context:

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

// Private function defined outside the class
function calculateCircleArea() {
  const radius = _radius.get(this);
  return (Math.PI * Math.pow(radius, 2)).toFixed(2);
}

class Circle {
  constructor(r) {
    _radius.set(this, r);
    // Bind the function to this instance and store it in the WeakMap
    _areaCalculate.set(this, calculateCircleArea.bind(this));
  }

  areaPrint() {
    // Retrieve and invoke the private method
    console.log("The area of Circle is: " + _areaCalculate.get(this)());
  }
}

let c = new Circle(4);
c.areaPrint(); // Output: The area of Circle is: 50.27

Key Fixes Explained

  • Storing the method in WeakMap: We explicitly add the calculation function to _areaCalculate for each instance in the constructor, so _areaCalculate.get(this) returns the actual method instead of undefined.
  • Invoking the method: Instead of just getting the method reference, we add () to execute it and get the calculated area value.
  • Accessing private radius: We use _radius.get(this) to fetch the stored private radius value, since this.radius isn't a defined property on the instance.

内容的提问来源于stack exchange,提问作者mrhrifat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:57:49