0

var foo= function(){
        this.a=1;
}
var obj= new foo;
console.log(obj);

how can object be created using foo instead of foo() ?

will the constructor will be called if i don't use parentheses ?

i think foo is also function in order to call the inside code we need parentheses but here why it doesn't need ?

Mahi
  • 1,707
  • 11
  • 22
  • 1
    [What is the difference between new Foo and new Foo() in javascript?](//stackoverflow.com/q/5227043) – Tushar Mar 29 '17 at 17:12
  • @Tushar then how internally it works ? – Mahi Mar 29 '17 at 17:12
  • http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.2 – Tushar Mar 29 '17 at 17:14
  • 1
    @Mahi: The language can do what the language is designed to do. There's no universal law that states what syntax must be used to invoke a function. Some languages let you call any function without parens, and even let you pass arguments that way. –  Mar 29 '17 at 17:15
  • 1
    See also ["Invoking a function without parentheses"](http://stackoverflow.com/questions/35949554/invoking-a-function-without-parentheses). It is wrong to think that JavaScript functions can only be invoked when parentheses are used. There are many counter examples. – trincot Mar 29 '17 at 17:16
  • http://stackoverflow.com/questions/11347046/constructor-invocation-without-parentheses This should answer your question. – karthikaruna Mar 29 '17 at 17:17

0 Answers0