Reputation: 5040
What will be the best way to get rid of repetitive code
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": {}
}
};
Upvotes: 3
Views: 54
Reputation: 5040
I have used T.J. Crowder code as below and its working fine for me
'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);
})();
OUTPUT:
test )
node js-class-test.js
SuccessResponse {
message: 'Message',
response_type_id: 1,
response_status_id: 2,
status: 3,
data: { name: 'vivek' } }
Upvotes: 0
Reputation: 1074674
Well, as you're using ES2015 (aka ES6), seems like class
might be an efficient option for you:
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?
}
}
Based on your reply to my comment on the question, that last one would be:
class SuccessResponse extends BaseErrorResponse {
constructor(msg, rti, rsi, st, data) {
super(msg, rti, rsi, st);
this.data = data;
}
}
Upvotes: 1
Reputation: 633
A little easier solution as for me is:
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;
};
Upvotes: 0
Reputation: 225005
You can just merge objects:
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 Object.assign(BaseErrorResponse(mes, rti, rsi, st), {
"invalid_params": ip
});
};
let SuccessResponse = function(mes, rti, rsi, st, data) {
return Object.assign(BaseErrorResponse(mes, rti, rsi, st), {
"data": {}
});
};
It might be a good idea to make these into actual constructors that inherit from each other, though.
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;
Upvotes: 4