Today in the process of developing the app in H5 encountered a JS problem, function of the implementation of the problem
There are two ways of declaring function functions in JS
var a=function () {...}
Or
function A () {...}
The first is called a variable reference function, because JS allows a function to be passed as a value to a variable, and the second is a direct declarative function
I always thought that these two kinds of differences, however, today in the use of the process found that when I use the first way to declare function, if the following:
document.getElementById ("xx"). AddEventListener ("tap", A), Var a=function () {...} Clicking Tap does not perform a
The reason is simple, if you use the first method, that is, the function is assigned as a value to the variable A, you must first declare a, and assign a value, a basic programming Knowledge: No declared variables can not be used (in memory does not exist), the variable declaration must be used before the In an instance, AddEventListener never get a!
However, if the second method, that is, the method of directly declaring the function, then the browser after the end of the DOM, the function is already there, can call
The var variable = method defines a function that cannot be called before the function is declared, only the function is declared and then called.
function methods define functions that can be called first and then declared.
var variable reference function and direct declaration function in JavaScript