A brief introduction to the different definition of JS function _ basic knowledge

Source: Internet
Author: User
Tags anonymous function definition

The function definition of JS has the following two kinds of ways:

(1) A typical function declaration

function Slide (arguments) { 
//...code
}

(2) Defining a function as a function expression

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

Although the above two ways are logically equivalent, there is a slight difference:

Difference One: The function in example one is loaded into the scope before the execution of the code, and example two is defined when the code executes to that line;
Difference Two: The function declaration assigns a name to the function, and the function expression creates an anonymous function and assigns the anonymous function to a variable;


Look at 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 expression is defined
var factorial = function (num) {
// ... code
}
//...code
console.log (anotherfactorial);//Output function () {}, anonymous function

The above JS function to define the way the difference is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.