在 Public 方法中获取私有方法值 JavaScript
Get Private Method Value at Public Method in JavaScript
我创建了一个 Class Circle
。这里
_radius
是私有参数
_areaCalculate
是私有方法
从私有方法计算值后 _areaCalculate
。我需要这个值到 public 方法 areaPrint
。但它告诉我 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()
如果坚持 OP 的方法,即通过原型方法利用弱映射访问 Circle
实例'“私有成员”,那么只需要将代码简化为单个参考地图和一个动态计算圆形实例面积的函数...
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();
... 或遵循 VLAZ 的建议并开始使用 private field declaration syntax for private instance fields.
编辑
来自下面与 Bergi 的基于评论的进一步讨论 ...
"Private methods, unlike private fields, are allocated on the prototype not on the instance, just the like their respective public counterparts" . – Bergi
... getComputedArea
的实现从本地辅助函数更改为 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();
我创建了一个 Class Circle
。这里
_radius
是私有参数_areaCalculate
是私有方法
从私有方法计算值后 _areaCalculate
。我需要这个值到 public 方法 areaPrint
。但它告诉我 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()
如果坚持 OP 的方法,即通过原型方法利用弱映射访问 Circle
实例'“私有成员”,那么只需要将代码简化为单个参考地图和一个动态计算圆形实例面积的函数...
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();
... 或遵循 VLAZ 的建议并开始使用 private field declaration syntax for private instance fields.
编辑
来自下面与 Bergi 的基于评论的进一步讨论 ...
"Private methods, unlike private fields, are allocated on the prototype not on the instance, just the like their respective public counterparts" . – Bergi
... getComputedArea
的实现从本地辅助函数更改为 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();