A function of js, the basic problem of variables. Come in and have a look.

function Foo(){
    bar=function(){console.log(1)};
    return this;
}

Foo.bar=function(){console.log(2)}
Foo.prototype.bar=function(){console.log(3)};
var bar = function(){console.log(4)}
function bar(){
    console.log(5);
}



new bar();
Foo.bar();
Foo().bar();
bar();
new Foo().bar()
//4 2 1 1 3

in this way, the function and variables will be improved after the browser is compiled, what will happen in the end? why will new bar print 4 console.log (5)? is it permanently overwritten?

Apr.19,2021

here is mainly function declaration promotion, function expression is not promoted. The promoted code is

var bar;
function Foo() {
    bar = function() { console.log(1) };
    return this;
}

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

Foo.bar = function() { console.log(2) }
Foo.prototype.bar = function() { console.log(3) };
bar = function() { console.log(4) }


new bar();//4
Foo.bar();//2  Foobar bar
Foo().bar();// 1
bar();// 1
new Foo().bar()//3

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

will improve the function, which is equal to
at the top and then overwritten by var bar = function () {}

.

1. Declare promotion, so var bar and the following function definition are both promoted
2. Execution continues after promotion, and bar is assigned to the function body of 4, so 5 is overwritten


A little bit:
the first step, the js interpreter parses and compiles the js code, and then you know the function declaration promotion step.
this is the source code:

new Foo().bar();
//  FoonewFoobarnew  Foo
// Foo bar barFoobarPrototypebar  3
Menu