Skip to content

Instantly share code, notes, and snippets.

@azproduction
Created June 11, 2011 17:35
Show Gist options
  • Save azproduction/1020780 to your computer and use it in GitHub Desktop.
Save azproduction/1020780 to your computer and use it in GitHub Desktop.
Lazy parser
var MyObject = function (url, req, res, path, body) {
this.res = res; // Object
this.req = req; // Object
this.body = body; // String
this.url = url; // Object
this.method = req.method; // String
this.get = url.query; // Object
this.path = path || {}; // Object
};
MyObject.prototype = {
constructor: MyObject,
/* Lazy Request body parser */
get post() {
var req = this.req;
// create own property to "owerwrite" prototype
this.post = req.body ? BodyParser.parse(req.headers['content-type'] || '', req.body) : {};
return this.post;
}
/* Other Lazy methods */
};
var myObject = new MyObject(/* ... */);
MyObject.post.pewpew; // lazy init uses getter get post()
MyObject.post.pewpew2; // now uses own parsed post property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment