Created
June 11, 2011 17:35
-
-
Save azproduction/1020780 to your computer and use it in GitHub Desktop.
Lazy parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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