If you want to combine objects, you can simply use the Object.assign() method:
let BaseError = function(message, typeId, statusId, statusCode) {
return {
"message": message,
"type_id": typeId,
"status_id": statusId,
"status_code": statusCode
}
};
let InvalidParameterError = function(message, typeId, statusId, statusCode, params) {
return Object.assign(BaseError(message, typeId, statusId, statusCode), {
"invalid_params": params
});
};
let SuccessMessage = function(message, typeId, statusId, statusCode, resultData) {
return Object.assign(BaseError(message, typeId, statusId, statusCode), {
"data": {}
});
};
Alternatively, you could consider converting these functions into actual constructors with inheritance relationships between them.
function BaseError(message, typeId, statusId, statusCode) {
this.message = message;
this.type_id = typeId;
this.status_id = statusId;
this.status_code = statusCode;
}
function InvalidParameterError(message, typeId, statusId, statusCode, params) {
BaseError.call(this, message, typeId, statusId, statusCode);
this.invalid_params = params;
}
InvalidParameterError.prototype = Object.create(BaseError.prototype);
InvalidParameterError.prototype.constructor = InvalidParameterError;
function SuccessMessage(message, typeId, statusId, statusCode, resultData) {
BaseError.call(this, message, typeId, statusId, statusCode);
this.data = resultData;
}
SuccessMessage.prototype = Object.create(BaseError.prototype);
SuccessMessage.prototype.constructor = SuccessMessage;