On the function definition and function expression in JS

Source: Internet
Author: User

In the process of beginner JS, it is always impossible to distinguish what is function expression and what is function definition, which is detrimental to the compaction of basic knowledge. Therefore, the access to information, carefully distinguish it.

A brief summary is as follows:

1. Difference One: function is defined by functions, and others are function expressions;

2. Difference two: The function expression can omit the function name.

         function functionname (formalparameterlist) {functionbody}          // functions Definition
         function [functionname] (formalparameterlist) {functionbody}        //functions expression

In the actual application of the function, the function definition cannot be executed immediately, causing a syntaxerror syntax error, so the shirt resolves it to a function expression. That is, in the function definition Money plus any other symbol, the most common and not weird notation is before the parentheses, for example: (function GetName () {}). So will everyone associate with anonymous functions? By the same time, you can learn another knowledge: ' (function () {}) ' and ' (function () {} ()) ' Two functions, what is the difference between these two functions? We can take a look at what they have in common, and it's clear that they all have parentheses, compared to normal functions, and that the parentheses have 2 functions in javascript: one is to establish the priority of the operation and the other as the grouping operator. This is certainly not the former usage, the second is the latter one. At this time

(function() {}) (), which evaluates a function expression with parentheses and then participates in "function call";

(function () {} ()), a function call is evaluated with parentheses and then executed due to the end of the statement.

On the function definition and function expression 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.