Why do variable declarations always overwrite function declarations?

Regardless of whether I define a function after a variable

var a = 1; function a() {}; typeof a // number 

Or if I define a function before a variable

 function a() {}; var a = 1; typeof a // number 

end result typeof always number

I found some explanation about the execution context at http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/

 Before executing the function code, create the execution context. ...... Scan the context for variable declarations: If the variable name already exists in the variable object, do nothing and continue scanning. 

but it does not work.

So how can I explain this?

+7
javascript function variables declaration
source share
3 answers

This is due to JavaScript variable changes . Try instead:

 var a = 1; var a = function() {}; typeof a // function 
+5
source share

You implicitly declare a variable several times using the function operator 'a () {};', which, as others have noted, raises this variable and behaves unexpectedly because the browser registers ads.

Behind the scenes, this statement creates an instance of the function object and assigns the result to the variable passed as the name of the function ( reference ), but this is done before explicit var declarations are executed, and thus overrides the implicit declaration. If you just do the following, it will work more intuitively:

 var a = 1; a = function(){}; console.log(typeof a); // function 

This is a better option than plural declaration of var in another answer from a logical point of view, because (although you can), it is not a good practice to declare a variable several times in any case.

To specifically answer the β€œwhy” to this question: so that you can use these types of operators to define functions and use them in your explicit declarations, as in

 var a = someFunction(); function someFunction(){ return 'someVal'; } 

If function statements were not parsed and picked up first, this would not be possible.

+1
source share

As already mentioned, this is due to how JavaScript works. The main problem that should be noted is that JavaScript will push the full definition of the function (along with the body of the function) to the top, but keep the variable initialized where it is (only the declaration is inserted).

So if you write this:

 var a = 1; function a () { } 

he will be translated into:

 var a; function a() { } a = 1; 

and if you write this:

 function a () { } var a = 1; 

he will be translated into:

 function a () { } var a; a = 1; 

So, no matter what you do, a = 1; will remain at the very bottom.

Please note that the above "translations" should be considered theoretically. JavaScript probably has a way to omit the var a; statement var a; if there is already a function declaration with the same name. And there can also be a certain order (functions rise to variables or vice versa). But all this does not affect the result of initializing the variable, which is the only part that is NOT raised at all.

0
source share

All Articles