0

I'm wondering if there is some best-practice for how to give behavior to object received from the outside (a db, the network, etc) in javascript (nodejs).

For instance, assume I have some getById(<Id>) function which fetch the right object from a db, how would you add a savemethod on the object returned by this function?

Here is what I would like to have:

var getById = function (id) {
   var obj = callSomeExternalCode(id);
   return obj;
};
var myObj = getById('1234');
myObj.aProperty = 'a_new_value';
myObj.save();

I would prefer to avoid pseudo-classical solution (anyway, I may adapt it to my style if you can provide me with a good one)

Edit: I forgot to mention, it would be very nice to have these methods defined in a separate 'parent' object (even if, I suppose, proto-inheritance is not the answer)

Thanks

3 Answers 3

1

it would be very nice to have these methods defined in a separate 'parent' object

Then just do so:

var parent = {
    save: function() {…}
};
function getById(id) {
    // There's a shortcut function called "extend" for this in many libs:
    // return extend(callSomeExternalCode(id), parent);
    // which does
    var obj = callSomeExternalCode(id);
    for (var p in parent)
        obj[p] = parent[p]; // copy
    return obj;
}

I suppose, proto-inheritance is not the answer

It could be, especially if there are more properties on parent than on the new obj. Then you could do (even without any pseudo-classical code):

function getById(id) {
    // return extend(Object.create(parent), callSomeExternalCode(id));
    // long:
    var obj = callSomeExternalCode(id),
        res = Object.create(parent);
    for (var p in obj)
        res[p] = obj[p]; // copy
    return res;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know that this may seems to be 'dirty' but as stated here, it's possible to swap the 'prototype' object in v8....does it look like too much dirty to you?
Yes, I think the non-standard __proto__ property is quite dirty - it won't work in IE as well. Of course a Object.setPrototypeOf might be handy sometimes, but I fear there is no standardized way...
oh, that won't be a problem, it's a node-only project :) Anyway, I agree...as long as it's not fully standardized I feel uncomfortable with it :D
1

why not pass the myObj to another factory / decorate object in front?

i.e.

myAugmentedObj = User.augment(myObj);
myAugmentedObj.save();

edit:

More concretely:

var UserModule = {
  save: function ( ... ) { ... },
  anotherFunction: function ( ... ) { ... }
};

var User = {
  augment: function (user) {
    return $.extend(user, UserModule);
  }
};

By the way,

I would actually make User a class, and have UserModule as part of it rather than a separate object, then create new objects as instances of User. But you explicitly mentioned avoiding classical inheritance.

1 Comment

Yes, this is what I was thinking about. The problem is how to implement the User.Augment function itself (I mean, copy from a template? some other technique?)
0

Something like this?

var getById = function (id) {
    var obj = callSomeExternalCode(id);

    obj.save = function() {
        // do something
    }

    return obj;
};

2 Comments

I edited the question to explain why I prefer to avoid this solution :) (anyway, isn't this duplicating the save function for each object?)
Yes, it is creating several save functions

Your Answer

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.