What's the recommended way of creating objects in NodeJS?

Viewed 85420

I'm building a composite for request module, however I'm not sure what's the best practice regarding building objects in JS for Node.

Option 1:

function RequestComposite(request) {
  return {
    get: function (url) { return request.get(url); }
  }
}
var comp = RequestComposite(request);
  • Note: I know that i should call a CB in async way, but for the ease of explaination I return it...

Option 2:

function RequestComposite(request) {
  this.request = request;
}

RequestComposite.prototype.get = function (url) { return this.request.get(url); };
var comp = new RequestComposite(request);

Option 3:

var RequestComposite = {
  init: function (request) { this.request = request; },
  get: function (url) { return request.get(url); }
}
var comp = Object.create(RequestComposite).init(request);

I tried to find my way around, however I got even more confused about how should I use objects...

Would the answer be different if I want to use objects for browsers?

Thanks.

3 Answers
Related