Can we omit parentheses when creating an object using the “new” operator?

前端 未结 6 2084
清酒与你
清酒与你 2020-11-22 00:41

I have seen objects being created this way:

const obj = new Foo;

But I thought that the parentheses are not optional when creating an objec

6条回答
  •  醉酒成梦
    2020-11-22 01:10

    I don't think there is any difference when you are using the "new" operator. Be careful about getting into this habit, as these two lines of code are NOT the same:

    var someVar = myFunc; // this assigns the function myFunc to someVar
    var someOtherVar = myFunc(); // this executes myFunc and assigns the returned value to someOtherVar
    

提交回复
热议问题