Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor invocation without parentheses [duplicate]

Is there any difference between

var obj1 = new Constructor;

and

var obj2 = new Constructor();

given that Constructor is a constructor function?

like image 726
Matt Fenwick Avatar asked Jul 05 '12 14:07

Matt Fenwick


People also ask

What happens when a method is accessed without the () parentheses?

When we call a function with parentheses, the function gets execute and returns the result to the callable. In another case, when we call a function without parentheses, a function reference is sent to the callable rather than executing the function itself.

Why do some methods not have parentheses?

Without parentheses you're not actually calling the function. A function name without the parentheses is a reference to the function. We don't use the parentheses in that code because we don't want the function to be called at the point where that code is encountered.


1 Answers

According to the MDN docs:

[...] "new foo" is equivalent to "new foo()", i.e. if no argument list is specified, "foo" is called without arguments.

like image 115
Matt Fenwick Avatar answered Oct 05 '22 15:10

Matt Fenwick