Why is "var" not used for customized constructors?


I noticed we do not use "var" for declaring customized constructors and I do not understand why they don't get explicitly declared in the same way that "Object( )" does.


function objectName(args*) { };


var objectName = function(args) { };


Hello @microblaster86319,
I think you confused the jQuery's function .val(); with JavaScript's var, didn't you?

So yes, it depends, how you create an object. But I guess you just misunderstood there something.

Hope this will help you understanding :slight_smile:


Sorry I meant "var" not "val"! Also, I haven't learned jQuery so that isn't it lol



Explains what happens with/out it


That was a very helpful article but I guess you have changed the nature of my question. Specifically, why are customized constructors made without declaring them explicitly (so according to the article they are implicitly global) whereas the object or literal constructors are always explicitly declared (using the "var" keyword). What is it about the Object( ) constructor that requires it be explicitly declared using "var" and "new" every time? Is this just a random note about Javascript and there is no reason to it?


Whoops, my bad.

Well, there's an article for that too:
The difference being that function declarations are hoisted, they are created before that scope starts executing.
They also get a name property.

new is how js knows to treat a function like a constructor, again, an article for that: