Hello, mu Sunny again to update, today, with everyone to talk about the function of the headache and function declaration, anyway, I looked at that high-level program, is not how to see too much, haha. I am a more basic person, join me in exploring the world of function expressions and function declarations.
First of all, look at their Yan value:
Functions Declaration : Function Function name () {}
function expression: Functions name (writable and not writable) () {};
(Do not write an anonymous function expression.) Write a named function expression. Recommended for Anonymous. )
Such a look looks like there is no difference, anyway Yan value is not high. So how do we distinguish between a function expression or a function declaration? The answer is to look at the context, the context, you know, like the context in the language composition.
function declaration: Functions aaa () {} (this is the function declaration, the context what also wood has.) )
function Expression :
var a = function aaa () {}: named function expression
var a = function () {}: anonymous function expression
(function aaa () {}): expression
Bitwise operators:
~funtion aaa () {}: Expression
-funtion aaa () {}: Expression
+funtion aaa () {}: Expression
! Funtion aaa () {}: Expression
(There are function expressions for all of the above representations)
OK, look at the performance of the different, we already know how to distinguish them, the following to understand their actual use of the difference .
1 function expressions can be executed directly after parentheses and function declarations are not allowed.
function aaa () {} (); cannot be executed. AAA () must be called through the function name;
var a = function aaa () {} (); Parentheses can be executed directly
2 function declarations can be parsed in advance. The expression is not available.
For example, whenever you declare a function, regardless of where the function is declared, it will be parsed in advance, so there is no problem with calling anywhere. But the function expression is not parsed in advance, so you can call it before the function expression. (on the parsing mechanism, I will carefully write the next chapter to share to you.) )
AAA (); Can call
A (); The call will go wrong
function aaa () {}; function declaration
var a = function aaa () {}; function expression
The main thing is the difference between the two, relative to the function declaration, the expression can let us not bother to think of so many function names, code is more simple, until I write a closure, will reflect its advantages.
OK, today's discussion is here, if there is a mistake, please correct me. I am a mu Qing. The next chapter is gone.
The difference between function expression and function declaration in JS