A class-A-class citizen of the JavaScript world-functions

Source: Internet
Author: User

Http://www.cnblogs.com/ciangcic/p/3526957.html

Brief introduction
In many traditional languages (c/c++/java/c#, etc.), functions are present as a second-class citizen, you can only declare a function with the keyword of the language and then call it, if you need to pass the function as a parameter to another function, or assign to a local variable, or as a return value, You need to go through a special way, such as function pointers (functions pointer), proxies (delegate).
In the JavaScript world, the function is a first-class citizen, it not only has all the traditional functions of the use of the way (Declaration and invocation), and can be like simple values, such as assignment, parameter, return, such a function is also known as the primary function (first-class functions). In addition, functions in JavaScript act as constructors for classes, and are also instances of a function class (instance). This multi-identity makes JavaScript's functions very important.
First, the JavaScript function entry level
JavaScript functions, like the general language, follow the principle of first declaration, and the function name can only contain letters, numbers, underscores, or $, and cannot begin with a number. Functions are commonly declared in the following two ways: 1//directly declares a function myfunc
2function myfunc (/* arguments */) {
3}
4
5//assign the anonymous function to the local variable MyFunc
6var MyFunc = function (/* arguments */) {
7}

Notice that there is a slight difference in the way the two functions are declared: The first is a named function at the time of declaration, whether it is declared before, after the call, or even where it will not be executed (for example, after a return statement or a branch that is never true), and is accessible throughout the scope The second way is by assigning an anonymous function to a variable, strictly speaking, it is not a function declaration, but rather a function expression, which cannot be accessed by any code until it is assigned. That is, the assignment must be completed before the call, or an error occurs when called: "typeerror:undefined is not a function". For example: 1myfunc1 (); Can be called normally because the MYFUNC1 uses the direct declaration method
2
3function myfunc1 () {
4}
5
6MYFUNC2 (); Error typeerror:undefined is not a function
7
8var MYFUNC2 = function () {
9};


A class-A-class citizen of the JavaScript world-functions

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.