Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is wrapping new within the constructor good or bad?

Tags:

javascript

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?

like image 334
Timothy Avatar asked May 17 '10 02:05

Timothy


People also ask

Can I use new in constructor?

Constructor functions or, briefly, constructors, are regular functions, but there's a common agreement to name them with capital letter first. Constructor functions should only be called using new .

What should not be done in constructor?

The most common mistake to do in a constructor as well as in a destructor, is to use polymorphism. Polymorphism often does not work in constructors !

Is it good practice to throw exception in constructor?

Throwing exceptions in a constructor is not bad practice. In fact, it is the only reasonable way for a constructor to indicate that there is a problem; e.g. that the parameters are invalid.

Should I initialize in constructor?

I recommend initializing variables in constructors. That's why they exist: to ensure your objects are constructed (initialized) properly. Either way will work, and it's a matter of style, but I prefer constructors for member initialization.


3 Answers

This is a defensive technique for when people forget to use the new operator in front of a 'class' function.
The logic is that if the function is called without new, then the global scope will still be the current scope (instead of the new instances), and so we just call the same function using the new operator.

But if you ask me, the correct thing would be to throw an error and actually let the developer know that it has made a mistake instead of just 'accepting' it.

But hey, I guess its according to the jQuery mantra:
'instead of enabling users to write quality code, enable/force them to write illogic and invalid code'.

like image 138
Sean Kinsey Avatar answered Oct 29 '22 12:10

Sean Kinsey


IMO I think that is a practical and completely valid defensive technique.

If you are building a constructor function, for sure you want to use the newly created object instance (this), and nothing good will happen if this points to the global object.

Fortunately in the future, in the ECMAScript 5 Edition on strict mode, this will simply contain undefined when a function is called with no base object nor the new operator...

See also:

  • Is JavaScript 's “new” Keyword Considered Harmful?
like image 40
Christian C. Salvadó Avatar answered Oct 29 '22 14:10

Christian C. Salvadó


You are returning an object from a function. I see no problem in that.

It is the same as doing:

function getObject(){ return new SomeObject; }

The only difference is that you are actually returning yourself. But, it doesn't really seem misleading if you clearly document it. People who are used to jQuery will probably praise you for the increased usability.

like image 43
Tyler Carter Avatar answered Oct 29 '22 12:10

Tyler Carter