1

Given the function A,

function A()
{
  ....
  console.log(1);
  this.a = 1;
}

If I do

var b = new A();

then b inherits properties of A, and

b.a == 1; //true

But if I do

var c = new A;

then c again inherits properties of A, and

c.a == 1; //true

Is there any difference between new A() vs new A?

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 2
    no, they are the same. `new` invokes the function even if you don't code invocation parens. – dandavis Jun 22 '16 at 06:58
  • @dandavis thanks, can you please point me towards the spec where this is mentioned? – gurvinder372 Jun 22 '16 at 07:01
  • "javascript contructor parentheses" --> [new MyObject(); vs new MyObject;](http://stackoverflow.com/questions/3034941/new-myobject-vs-new-myobject) – GolezTrol Jun 22 '16 at 07:02
  • @gurvinder372, Just `console.log()` both the statements... – Rayon Jun 22 '16 at 07:03
  • @GolezTrol, Did I even mention anything about _specs_ ? Come on! – Rayon Jun 22 '16 at 07:05
  • @GolezTrol, Come on! OP never mentioned anything about _specifications_ in the question.. I __DID__ read the question, also consider the last sentence in the question which states _"Is there any difference between `new A()` vs `new A`?"_ and I seriously doubt that my comments about `console.log` is not helpful at all for that cause... – Rayon Jun 22 '16 at 07:17
  • 1
    @Rayon Thanks, your comment is helpful to the original question but by the time your comment came dandavis had already given an authoritative answer (comment) to the same and after that I was simply looking for a spec to support the same. – gurvinder372 Jun 22 '16 at 07:19
  • i don't know about the spec, but Crockford/jslint wants you to include empty parens for readability, not compatibility... – dandavis Jun 22 '16 at 15:00

2 Answers2

1

new operator (new constructor[([arguments])]):

new Foo is equivalent to new Foo(), i.e. if no argument list is specified, Foo is called without arguments

CD..
  • 72,281
  • 25
  • 154
  • 163
1

When you call new A(); You actually calling an empty constructor to initialize the values which are automatically created for you during run tym... Well unless you don't have any arguments to pass there is no diff. Since both are internally calling empty constructors.

Alaap Dhall
  • 351
  • 1
  • 3
  • 16