什么是摆脱重复代码的最佳方法
let BaseErrorResponse = function(mes, rti, rsi, st) {
return {
"message": msg,
"response_type_id": rti,
"response_status_id": rsi,
"status": st
}
};
let InvalidParamResponse = function(mes, rti, rsi, st, ip) {
return {
"message": msg,
"response_type_id": rti,
"response_status_id": rsi,
"status": st,
"invalid_params": ip
}
};
let SuccessResponse = function(msg, rti, rsi, st, data) {
return {
"message": null,
"response_type_id": null,
"response_status_id": null,
"status": null,
"data": {}
}
};
答案 0 :(得分:4)
你可以merge objects:
function BaseErrorResponse(mes, rti, rsi, st) {
this.message = msg;
this.response_type_id = rti;
this.response_status_id = rsi;
this.status = st;
}
function InvalidParamResponse(mes, rti, rsi, st, ip) {
BaseErrorResponse.call(this, mes, rti, rsi, st);
this.invalid_params = ip;
}
InvalidParamResponse.prototype = Object.create(BaseErrorResponse.prototype);
InvalidParamResponse.prototype.constructor = InvalidParamResponse;
function SuccessResponse(mes, rti, rsi, st, data) {
BaseErrorResponse.call(this, mes, rti, rsi, st);
this.data = data;
}
SuccessResponse.prototype = Object.create(BaseErrorResponse.prototype);
SuccessResponse.prototype.constructor = SuccessResponse;
但是,将这些构建成相互继承的实际构造函数可能是个好主意。
hasNext()
答案 1 :(得分:1)
好吧,当您使用ES2015(又名ES6)时,似乎class
可能是您的有效选择:
class BaseErrorResponse {
constructor(mes, rti, rsi, st) {
this.message = msg;
this.response_type_id = rti;
this.response_status_id = rsi;
this.status = st;
}
}
class InvalidParamResponse extends BaseErrorResponse {
constructor(mes, rti, rsi, st, ip) {
super(mes, rti, rsi, st);
this.invalid_params = ip;
}
}
class SuccessResponse extends BaseErrorResponse {
constructor(msg, rti, rsi, st, data) {
super(null, null, null, null); // Why the nulls when you're passing
// those args in?
this.data = {}; // Didn't you mean = data here?
}
}
根据您对我对该问题的评论的回复,最后一个是:
class SuccessResponse extends BaseErrorResponse {
constructor(msg, rti, rsi, st, data) {
super(msg, rti, rsi, st);
this.data = data;
}
}
答案 2 :(得分:0)
对我来说更简单的解决方案是:
var BaseErrorResponse = function(mes, rti, rsi, st) {
return { mes, rti, rsi, st };
};
var InvalidParamResponse = function(mes, rti, rsi, st, ip) {
var response = BaseErrorResponse(mes, rti, rsi, st);
response.invalid_params = ip;
return response;
};
var SuccessResponse = function() {
var response = BaseErrorResponse(null, null, null, null);
response.data = {};
return response;
};
答案 3 :(得分:0)
我使用过 T.J. Crowder 代码如下,对我来说工作正常
'use strict';
class BaseErrorResponse {
constructor(msg, rti, rsi, st) {
this.message = msg;
this.response_type_id = rti;
this.response_status_id = rsi;
this.status = st;
}
}
class InvalidParamResponse extends BaseErrorResponse {
constructor(mes, rti, rsi, st, ip) {
super(mes, rti, rsi, st);
this.invalid_params = ip;
}
}
class SuccessResponse extends BaseErrorResponse {
constructor(msg, rti, rsi, st, data) {
super(msg, rti, rsi, st); // Why the nulls when you're passing
// those args in?
this.data = data; // Didn't you mean = data here?
}
}
(()=> {
let sr = new SuccessResponse('Message', 1, 2, 3, {name: 'vivek'});
console.log(sr);
})();
<强>输出:强>
测试)
node js-class-test.js
SuccessResponse {
message: 'Message',
response_type_id: 1,
response_status_id: 2,
status: 3,
data: { name: 'vivek' } }