javascript - 在 JS ES6 类中强制使用 setter 而不是直接赋值
问题描述
我有一个看起来像这样的 CameraBuilder 类:
class CameraBuilder {
constructor() {
if (arguments.length) {
throw new Error('[CameraBuilder constructor ERROR] class constructor does not accept parameters.');
}
this.camera = {};
}
withFarmLabel(farmLabel) {
this.camera.farm_label = farmLabel;
return this;
}
// more methods here
build() {
const missingProps = [];
if (!this.camera.farm_label) {
missingProps.push('\nMissing farm_label property. Use the withFarmLabel method in order to assign it.');
}
// more validations like the one above here
if (missingProps.length) {
const errorMsg = missingProps.join('');
throw new Error(`[CameraBuilder build ERROR] ${errorMsg}`);
}
return this.camera;
}
}
由于我的大部分验证都在build()
方法上,并且其中一些方法上存在一些与用户如何构建 CameraBuilder 实例相关的业务逻辑,因此我不希望任何人cameraBuilderObj.camera
直接分配。有什么方法可以强制使用 Class 方法来将属性分配给 Camera 对象?
解决方案
您可以通过将camera
属性放在它前面来将其设为私有#
,确保只有CameraBuilder
' 的内部可以引用它:
class CameraBuilder {
#camera = {};
constructor() {
if (arguments.length) {
throw new Error('[CameraBuilder constructor ERROR] class constructor does not accept parameters.');
}
}
withFarmLabel(farmLabel) {
this.#camera.farm_label = farmLabel;
return this;
}
// more methods here
build() {
const missingProps = [];
if (!this.#camera.farm_label) {
missingProps.push('\nMissing farm_label property. Use the withFarmLabel method in order to assign it.');
}
// more validations like the one above here
if (missingProps.length) {
const errorMsg = missingProps.join('');
throw new Error(`[CameraBuilder build ERROR] ${errorMsg}`);
}
return this.#camera;
}
}
const c = new CameraBuilder();
c.withFarmLabel('label');
console.log(c.camera);
console.log(c.build().farm_label);
推荐阅读
- python - 为什么在这种情况下需要使用星号?
- flutter - 当 sliver child 在屏幕中央时如何触发函数调用?
- mysql - 数据库设计 - 使用“所有者”、“合作伙伴”、“员工”为“公司”建模
- mysql - Laravel 6 多对多关系
- javascript - 单击按钮时出现类型错误,说明该功能未定义?
- c++ - 为什么使用一个构造函数初始化引用成功但另一个构造函数失败
- reactjs - 反应原生背景颜色仅覆盖一半屏幕,如何解决?
- python - Selenium Driver:如何找到网页加载后添加的元素?
- javascript - 在制表器中从 json 加载数据?
- python - 我的结果是四舍五入的,我想要完整的答案