What is the difference between "new class" and "new class" () in javascript

function ClassA(){ this.value = 5; } var obj1 = new ClassA; var obj2 = new ClassA(); console.log(obj1.value); console.log(obj2.value); 

Both print "5" in the console. What is the difference between the two methods (other than including arguments for the constructor)?

+1
source share
2 answers

Brackets are optional β€” if you do not have any constructor parameters, they may be omitted.

Oddly enough, semicolons are also (sometimes) optional due to automatic semicolon insertion ( source , more ). This sparked a lengthy debate earlier this year and sparked a response from Douglas Crockford (JSON / JSLint inventor, JS guru).

+4
source

There is no difference.

If there are no arguments, and new exists, then the brackets are optional.

+1
source

All Articles