So basically, what I'm trying to do is to create a dummy function on an object that does nothing, its only purpose is to be called by another function (I'm passing the object which has this dummy function as a parameter of another function).
Here is a sample code of what I'm trying to achieve:
Calling function
exports.user = function(req, res, next, id) {
if (!id) {
res.status(401);
return next(new Error('Invalid ID.'));
}
// get user from db assign it to a req object property
....
req.user = user;
next(false);
};
Specially crafted object
var req = {
status: function() {}
};
users.user(req, null, next, id);
Then the next
callback will use the req.user
value.
I can go and redefine another version of exports.user
function, which does not call status
but I feel like I'm repeating myself.
My question is: is this not advised in JavaScript? or is there a better way to do it?
This does look like a mixture of Adapter pattern, or Null Object pattern.
Aucun commentaire:
Enregistrer un commentaire