Yesterday, mocha surprised me by telling me that my application had a
global variable leak.  I tracked it down to calling the mongodb
ObjectID function without the new keyword.  Aside from from the leak,
the function was working as expected in our application.  But, it made
me wonder if this issue should be mitigated when possible?

If I have a method that is always intended to be called with the new
keyword, is there any harm in placing this at the beginning of the
method?

function someFunction() {
  if(this == global) return new someFunction();
  //...
}

Alternatively, I suppose one could also throw an error instead of
making the recursive call with the new keyword.

Is either approach a good idea?  Why or why not?

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to