我已经对Java有一定的经验,并且习惯于使用DDD方法进行建模。因此,现在我从JS和NodeJS开始,我想知道哪种是在Javascript中使用类似方法(没有ES6)进行设计的最佳实践。
这里有一个我要尝试做的事的示例,但我不知道这些是否是更好的方法。我感谢一些意见。
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;