I have used JavaScript for many years and wrote countless functions, but today I really understand the difference between the definition of the two functions. It is really a tragedy. I wrote down this essay and reminded myself to lay the foundation. It's right.
Usually we see the following two definition functions:
Copy code code as follows:
// functional sentence
Function Fn (STR)
{{
console.log (STR);
};
// expression definition
var FNX = Function (STR)
{{
console.log (str+ 'from fnx');
};
In the past, I used my fingers to use the two -_- ||. Today I saw the JS foundation, and finally solved the confusion in their hearts:
Both methods create new function objects, but the function name of the functional statement statement is a variable name, variables point to function objects, and like VAR declares variables. The function definition statement is displayed in advance to the script or function of the function. At the top, they are visible in the entire script and functions, but using VAR expressions define functions. Only variable declarations are early. The initialization code of variables is still in the original position. Both are advanced, so we can use it before declaration.
The code example is as follows:
Copy code code as follows:
console.log (typeof (fn)); // Function
fn ('abc'); // abc
console.log (Typeof (Fnx)); // Undefined
if (fnx)
fnx ('abc'); // will not exceed
else
console.log ('fnx is undefined'); // fnx is undefined
// functional sentence
Function Fn (STR)
{{
console.log (STR);
};
// expression definition
var FNX = Function (STR)
{{
console.log (str+ 'from fnx');
};
The code is simple. I hope that the students who do not understand the difference between the two can gain something.