If you put ()
or (optionalArgumentsHere)
(some functions take arguments, some don't) after a function it means it is a function call, and it will be executed. If you want to assign the function itself to a variable, you need to omit the ()
. It is possible because functions are objects in JavaScript (which is not true in every language).
So what happens here is that you declare a variable (you actually declare it earlier because of hoisting, as explained by Pablo), you execute new Person()
and assign the result to the variable.
If you called some function like
function fun() {
return 5;
}
var x = fun();
You would be assigning the return value of the function to the variable. But your case is special, because you use new
. That means Person
is a constructor, and it is used to create a new object of type Person
. Inside that constructor, you use the this
keyword to attach properties to the newly created object. The new Person()
call returns the object, even though return
is not called explicitly.