I already have some experience with Java and I'm used to modeling with a DDD approach. Therefore, now I'm beginning with JS and NodeJS and I'd like to know which is the best practice to design with a similar approach in Javascript (without ES6).
Here I have an example of what I'm trying to do, but I don't know if these is the better way. I appreciate some opinions.
function Purchase(pCustomer, pOrders, pPayment, pTotal) {
let idPurchase = PurchaseId.purchaseId();
let customer = pCustomer;
let payment = pPayment;
let orders = pOrders;
let deliveryAddress;
let billingAddress;
let total = pTotal;
// Return an object exposed to the public
return {
setOrders: function (o) {
orders = o
},
setDeliveryAddress: function (address1, address2, zipCode, city) {
deliveryAddress = new Address(address1, address2, zipCode, city);
},
setBillingAddress: function (address1, address2, zipCode, city) {
billingAddress = new Address(address1, address2, zipCode, city);
},
getId: function () {
return idPurchase;
},
getOrders: function () {
return orders;
},
getCustomer: function () {
return customer;
},
toJSON: function () {
return {
idPurchase: idPurchase,
customerOnPurchase: customer.toJSON(),
deliveryAddress: deliveryAddress.toJSON(),
billingAddress: billingAddress.toJSON(),
total: total,
}
}
}
}
module.exports = Purchase;