The difference between Class (), the new class, and the new class ()

What is the difference between Class() and new Class , new Class() ? I did a test, and later, it seemed to be faster.

http://jsperf.com/object-initilzation

I read that there is no difference, but it seems to be.

+4
source share
3 answers

Class()

Invokes a function. Do not use this for constructor functions.

new Class and new Class()

There is no difference between the two, how to instantiate a new instance of the class. Paranas are optional if no arguments are passed and the new keyword is used.

+3
source

Class() is a misuse of the constructor function. When you call it like this, it has a global scope as a context object and does not create a new instance of the class. I believe that the main difference between new Class and new Class() is to optimize some of the arguments used by some javascript engines

0
source

If you have a class like this:

 function Test(propVal){ this.prop = propVal; } 

You will get the following results:

Calling Test () This will return "undefined" since Test works as a constructor

Calling a new test (5) You will receive a new instance of Test, and this instance will have its "prop" equal to 5

Calling a new test You will receive a new instance of Test, and this instance will have its "prop" set to undefined

Hope this helps.

Thanks,

0
source

All Articles