"Function declarations" and "function expressions" are actually the way to define functions
function declaration:
Function name () {...}
a function defined by the above method is a function declaration
function Expression: Functions are defined by function expressions in more ways
1. Var a = function test () {...} // This is a named function expression
var A = function () {...}//This is an anonymous functional expression
2. functions defined by function declaration enclosed in a pair of parentheses, which also forms the function expression
(function test () {...}) This is also the function expression
3. and "function declaration" The pre-bitwise operator can also form a function expression
For example: These are function expressions
~function Test () {...} +function Test () {...} -function Test () {...}! function Test () {...}
difference: What is the difference between a function declaration and a function expression??
1. Function expressions can be executed directly after the parentheses (this is the function self-executing), and the function declaration can not
For example:
var a = function test () { alert ("Hello");}//This is a function expression that is appended to the expression to automatically execute the function.
var a = function test () { alert ("Hello");//Refresh page pops up Hello} ();
And the way the function is declared is not allowed to execute, for example, the browser will prompt that the notation is incorrect.
function test () { alert ("Hello");} ();
2. Function declarations can be pre-parsed, and function expressions do not; If you do not understand JS pre-analytic small partners, please refer to another article "JS Pre-analysis"
For example:
Window.onload = function () { test (); function test () {//Functions Declaration alert ("Hello");} }
A function that is defined by a function declaration can be pre-parsed, so calling the test () function before function test () naturally pops up Hello,
Window.onload = function () { a (); var a = function () {//Functions Expression alert ("Hello");} }
A function defined by a function expression cannot be pre-parsed, so calling a () before the function will result in an error.
let's look at an example:
Window.onload = function () { a (); if (true) { function A () { alert ("1"); } } else{ function A () { alert ("2"); } }}
We were going to let ifset up when the popup1, does not set up popup2
But because JS will be pre-parsed, so it always pops up 2, but using a function expression can avoid this situation
Window.onload = function () { if (true) { var a = function () { alert ("1"); } } else{ var a = function () { alert ("2"); } } A (); }
since the function expression will not be pre-parsed, it will follow normal logic, and naturally it will pop up 1
so remember : When writing a program, if you need to decide to perform different functions according to different criteria, then you must define the function in the form of "function expression" . This will prevent many errors from occurring.
JS function-A grudge between a function declaration and a function expression