Is wrapping new within the constructor good or bad?

Posted by Timothy on Stack Overflow See other posts from Stack Overflow or by Timothy
Published on 2010-05-17T02:38:38Z Indexed on 2010/05/17 2:40 UTC
Read the original article Hit count: 278

Filed under:
|

I watched John Resig's Best Practices in JavaScript Library Design presentation; one slide suggested "tweaking" the object constructor so it instantiates itself.

function jQuery(str, con) {
    if (window === this) {
        return new jQuery(str, con);
    }
    // ...
}

With that, new jQuery("#foo") becomes jQuery("# foo").

I thought it was rather interesting, but I haven't written a constructor like that in my own code.

A little later I read a post here on SO. (Sorry, I don't remember which or I'd supply a link. I will update the question if I can find it again.) One of the comments said it was bad practice to hide new from the programmer like that, but didn't go into details.

My question is, it the above generally considered good, bad, or indifferent, and why?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about best-practices