The difference between function expression and function declaration in JS

Source: Internet
Author: User
Tags bitwise operators

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.