Difference between the function declaration and function expression in javascript, javascript expression

Source: Internet
Author: User

Difference between the function declaration and function expression in javascript, javascript expression
1. function expression definition in js

Expression is a phrase in JavaScript, and javascript calculates it (evaluate) to produce a result. Constants in a program are the simplest expressions. A variable name is also a simple expression. Its value is the value assigned to the variable. A complex expression is composed of simple expressions.
-- From javascript authoritative guide

How to define a classic function expression:

// Declaration of the function expression. The value of the expression is the newly defined function var expressFunc = function (){};

The function name is a required part of the function declaration statement. Its purpose is like the name of a variable. The newly defined function object will assign a value to this variable. For a function definition expression, this name is optional: If it exists, the modified name only exists in the function body and indicates the function object itself.
Note:
Defining a function using an expression only applies to it as a part of a large expression, such as defining a function during assignment and call.

// Declaration of the function expression. The value of the expression is the newly defined function var expressFunc = function () {}; // function declaration function abc () {}// function declaration of input parameters function abc2 (test) {}// function expression Entry and Exit parameter var expressTest = function (test) {}; // function expression can contain a name, here, the result function is actually assigned to expressTestvar expressTest = function evalu (test) {if (test) {return 2 ;}else {return-1 ;}}; // console. log (evalu (); // incorrect call method console. log (expressTest (); // The function expression is used as a parameter to pass to other functions. sort (function (a, B) {return a-B ;})

Define a function in the form of a function expression. The function name is optional. If the function name is defined, the function name becomes a local variable inside the function (suitable for recursion ). Function Definition expressions are especially suitable for defining functions that only need to be used once.

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.