Use functional numeric face quantity and functional declaration to declare two functions of the same name at the same time, why does the result only execute the function of function numeric face quantity declaration?

question:

var getName = function(){alert(1)};
function getName(){alert(2)};

getName();// 1

or

function getName(){alert(2)};
var getName = function(){alert(1)};

getName();// 1

Why?

Jun.09,2021

first the function is declared in advance
is equivalent to first declaring the getName function and then getName overwriting


1. Declarative functions are declared and defined in advance
2. During execution, you can reassign


var getName = function(){alert(1)};
function getName(){alert(2)};

getName();// 1

it can be understood that the above part is pre-processed.

var getName;
function getName(){alert(2)};

getName = function(){alert(1)};
getName();// 1

the test example is

getName();
function getName(){alert(2)};

the second can be understood as, again, the first part is pre-processing.

function getName(){alert(2)};
var getName;

getName = function(){alert(1)};
getName();// 1

that's why it's always 1

.
Menu