javascript - 重构多种方法使用的相似代码
问题描述
我有这样的情况:有两种方法(我无法更改,因为它们来自抽象类)from()和to()必须执行相同的代码,只有很小的区别。我创建了第三种方法,并使用了一个布尔变量来知道这两种方法中的哪一种称为第三种方法。现在,有没有办法以更干净的方式写这个?
protected from(x: any): any[] {
return this.convertArray(true, x);
}
protected to(y: any): any[] {
return this.convertArray(false, y);
}
private convertArray(isFrom: boolean, inArray: any[]): any[] {
const array = [];
if (this.types.length === 1) {
// ... some code here ...
array.push(isFrom ? adapter.from(value) : adapter.to(value));
} else {
// ... some code here ...
array.push(isFrom ? adapter.from(inArray[i]) : adapter.to(inArray[i]));
}
return array;
}
解决方案
您还有其他一些选择:
您可以使用字符串作为方法名称和括号表示法从以下位置获取方法
adapter
:private convertArray(isFrom: boolean, inArray: any[]): any[] { const array = []; const method = isFrom ? "from" : "to"; // *** if (this.types.length === 1) { // ... some code here ... array.push(adapter[method](value)); // *** } else { // ... some code here ... array.push(adapter[method](inArray[i])); // *** } return array; }
您可以使用通过创建的包装函数
bind
(因此this
在调用中是adapter
):private convertArray(isFrom: boolean, inArray: any[]): any[] { const array = []; const method = (isFrom ? adapter.from : adapter.to).bind(adapter); // *** if (this.types.length === 1) { // ... some code here ... array.push(method(value)); // *** } else { // ... some code here ... array.push(method(inArray[i])); // *** } return array; }
或(称其为“2(b)”)通过箭头函数:
private convertArray(isFrom: boolean, inArray: any[]): any[] { const array = []; const op = isFrom ? value => adapter.from(value) // *** : value => adapter.to(value); // *** if (this.types.length === 1) { // ... some code here ... array.push(op(value)); // *** } else { // ... some code here ... array.push(op(inArray[i])); // *** } return array; }
或者直接通过以下方式使用该方法
.call
:private convertArray(isFrom: boolean, inArray: any[]): any[] { const array = []; const method = isFrom ? adapter.from : adapter.to; // *** if (this.types.length === 1) { // ... some code here ... array.push(method.call(adapter, value)); // *** } else { // ... some code here ... array.push(method.call(adapter, inArray[i])); // *** } return array; }
推荐阅读
- uml - 我应该设计多少个活动图?
- json - 我收到“无效的模板属性或属性”模板验证错误:无效的模板属性或属性 [IPAssoc,IPAddress]”
- angular - Angular Universal 构建挂在带有 API 请求的预渲染页面上
- python - 在 Python 中使用堆栈解决迷宫 - 我的算法是否正确?
- python-3.x - 使用 np.polyfit 计算并绘制沿多项式回归产生的曲线的切线
- javascript - 如果日期是今天,则添加 jquery 类
- algorithm - 拉斯维加斯算法的真实例子是什么?
- linux - 有没有办法找到 GCP VM 启动脚本的返回码
- java - 切换到不同的屏幕 JavaFX 和 FXML
- javascript - 刷新数据而不加载页面php sql