Why is this warning 5?

A curious bit of code is here ...

var x = 5;

function fn() {
    x = 10;
    return;
    function x() {}
}
fn();
alert(x);

Here jsFiddle

Is it function x() {}called at all after return? Why not warn 10?

+5
source share
7 answers

function x() {} raised to the beginning of the field fn, and this actually makes the xlocal variable before x = 10;evaluated.

Function not set to 10.

: . x 10. var , , , x, 10.

MDN ( ):

:

  • :
    • var,
    • "" , vars

  • -
  • redeclarations no-ops
+10

x float , 10

+3

x fn; x. , ​​ x. , , x 10, x 10.

+2

5:

var x = 5;

function fn() {
    x = 10;
    return;
    var x;
}
fn();
alert(x);​

, x. return - - .

, 10, x .

+2

, JavaScript. , . ( ).

, :

 var x = 5;

 function fn() {
     var x;
     x = function () {}
     x = 10;
     return;
 }
 fn();
 alert(x);

x , , x, .

+1

, fn , x . .

0

X fn() - x - var

x() {}, x 10

0

All Articles