johny
johny

Reputation: 13

Javascript ways of instantiation the class and directly calling the method

What is the correct way of creating instance of the class and directly call the method?

normally I use:

var obj = new Class();
obj.method();

if I want to call the method without creating a new variable which way is correct and what is difference between these options?

  1. new Class().method() or
  2. (new Class()).method()

When and why should I use 1. or 2. option?

thanks a lot

Upvotes: 1

Views: 34

Answers (1)

Quentin
Quentin

Reputation: 943686

There is no practical difference between those two options.

The parentheses are redundant, just as they are in the expression (1 + 2) + 3.

Upvotes: 1

Related Questions