The problem of function declaration, function variable and constructor call

the code is as follows

function Foo(){
    getName = function(){
        console.log(1);
    };
    return this;
}
Foo.getName = function() {
    console.log(2);
}
Foo.prototype.getName = function(){
    console.log(3);
}

var getName = function(){
    console.log(4);
};

function getName() {
    console.log(5);
}

Foo.getName(); // 
getName(); // 
Foo().getName(); // 
getName(); // 
new Foo.getName(); // 
new Foo().getName(); //  
new new Foo().getName(); //

what I want to ask now is: can"t new Foo () be written as new Foo without parentheses? why is the result different between question 5 and question 6? and the last question directly does not understand why the result is like this. Thank you for your generous advice

.
May.03,2022

I. As to why No. 5 and No. 6 are different, in this case, there is a difference between whether or not to wear parentheses.

https://codeshelper.com/q/10...

I. Last question

new Foo (). GetName ();
equates to const a = new Foo (); a.getName (); that is Foo.prototype.getName ()

new new Foo (). GetName ();
as const A = new Foo (). GetName; new A ();
then as const A = Foo.prototype.getName; new A ();

in addition, operator precedence


1, question 5 and question 6 have different results.
has different priorities
new Foo.getName (); is similar to new (Foo.getName) ()
new Foo (). GetName (); is similar to (new Foo ()). GetName ()
2, the last question is a better understanding of new ((new Foo ()) .getName) ()

Menu