Function modes in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the function modes in JavaScript, this article describes the syntax, expression, naming expression, declaration, declaration, and expression for creating a function, function upgrade, and real-time function mode, if you need it, you can refer to the JavaScript design pattern to improve code reusability and readability, making it easier to maintain and expand the code.

In javascript, a function is a type of object, which means that it can be passed to other functions as a parameter. In addition, a function can also provide a scope.

Syntax for creating a function

Name function expression

The code is as follows:


// Name function expression
Var add = function add (a, B ){
Return a + B;
};


Function expression

The code is as follows:


// Also known as Anonymous functions
Var add = function (a, B ){
Return a + B;
};


The value assigned to the add variable is the function definition itself. In this way, add becomes a function that can be called anywhere.

Function declaration

The code is as follows:


Function foo (){
// Code here
} // You do not need a semicolon here


In the trailing semicolon, the function expression should always use the semicolon, and the declaration of the function does not need to end with the semicolon.

Function declaration and expression

Hoisting)

The behavior of the function declaration is not equivalent to the name function expression. The difference is that the hoisting behavior. See the following example:

The code is as follows:


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.