I watched John Resig Best Practices in JavaScript Library Design ; one slide suggested setting up the constructor of the object so that it would instantiate itself.
function jQuery(str, con) { if (window === this) { return new jQuery(str, con); }
In this case, new jQuery("#foo") becomes jQuery("#foo") .
I thought this was pretty interesting, but I did not write such a constructor in my own code.
A little later I read the post here about SO. (Sorry, I donโt remember which link I will put. I will update the question if I can find it again.) One of the comments said that it was bad practice to hide new from such a programmer, but did not go into details.
My question is that the above is usually considered good, bad or indifferent, and why?
javascript
Timothy
source share