Differences between JS function definitions and js function definitions

Source: Internet
Author: User

Differences between JS function definitions and js function definitions

There are two methods to define JS functions:

(1) typical function declaration

Function slide (arguments) {//... code}

(2) define a function in the form of a function expression

Var slide = function (arguments) {//... code}

Although the above two methods are logically equivalent, they are slightly different:

Difference 1: Functions in Example 1 will be loaded into the scope before code execution, while in example 2, they will be fixed only when the code is executed to that line;
Difference 2: The function declaration will specify a name for the function, and the function expression is to create an anonymous function, and then assign this anonymous function to a variable;


Take the following example:

Function factorial (num) {if (num <= 1) {return 1;} else {return num * arguments. callee (num-1) ;}}var anotherFactorial = factorial; factorial = null; console. log (anotherFactorial); // output factorial () {}. If a function name is defined as a function expression, var factorial = function (num ){//... code }//... codeconsole. log (anotherFactorial); // output function () {}, anonymous function

The differences between the above JS function definition methods are all the content shared by the editor. I hope to give you a reference and support for the help house.

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.