Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between new Foo and new Foo() in javascript?

Tags:

javascript

I know the difference between two in C++, but don't know if it's the same for JS also

like image 401
Ankit Avatar asked Mar 08 '11 00:03

Ankit


2 Answers

From my experience there is no difference other then with new Foo you can't pass any parameters and with new Foo() you can.

like image 61
McKayla Avatar answered Nov 11 '22 17:11

McKayla


From the ECMAScript Language Specification for new:

new NewExpression:

Call the [[Construct]] method on Result(2), providing no arguments

new MemberExpression Arguments

Call the [[Construct]] method on Result(2), providing the list Result(3) as the argument values.

It's simply a matter of whether the constructor receives any arguments or not.

like image 32
Tim Cooper Avatar answered Nov 11 '22 19:11

Tim Cooper