2
\$\begingroup\$

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;
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 16, 2018 at 17:05
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Based on functions like getId and toJSON, it seems like you would naturally want to have a base class for your models. As such, it would make sense to write your functions in a way that support prototypical inheritance e.g.

Base Model

function Entity(id) {
 this._id = id;
}
Entity.prototype = {
 function getId() {
 return this._id;
 }
 ...
}
module.exports = Entity;

Derived Model

var Entity = require('./entity');
var util = require('util');
function Purchase(customer, orders, ...) {
 Entity.call(this, PurchaseId.purchaseId());
 this._customer = customer;
 this._orders = orders;
 ...
}
util.inherits(Purchase, Entity);
Purchase.prototype.getCustomer = function() {
 return this._customer;
}
Purchase.prototype.getOrders = function() {
 return this._orders;
}
...
module.exports = Purchase;

Usage

var purchase = new Purchase(...);
console.log(purchase.getId()); // Purchase will inherit everything from Entity
console.log(purchase.getCustomer()); // and have it's own functions too

Note - notice in base we set the prototype directly but in the derived we modify it? This is deliberate because in the base class the prototype is currently empty so it's fine to completely overwrite. If we did the same thing on the derived class then we'd lose the base prototype therefore we simply append to the existing prototype.

dfhwze
14.1k3 gold badges40 silver badges101 bronze badges
answered Jul 25, 2018 at 7:44
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.