The declaration of the JavaScript base function

Source: Internet
Author: User

1. The important position of the function

The function (functions) plays an important role in JavaScript for two reasons:
    • They're a special kind of object.
    • They provide scope
It is said that functions are special objects in JavaScript because:
    • The function object can be created dynamically during the execution period of the program;
    • function objects can be assigned to variables, which can be passed to other variables, and can be deleted;
    • A function object can be used as a formal parameter of other functions, or as a return value of a function;
    • A function object can have its own properties and methods (methods).
For example, (you can do this entirely in real programming) for JavaScript function A, it is an object; it has its own properties and methods, this method is a function, called function B; function B takes another function C as a formal parameter, and when B finishes, it returns a new function D. This is where JavaScript functions are powerful and flexible.
The function can also use its constructor function to be new (but we do not recommend it):
antipattern//for demo purposes Onlyvar add = new Function (' A, B ', ' return a + B '); Add (1, 2); Returns 3

In the above code, add is an object because it is the result of the new operation, but it is also a function because it can perform a calculation task by passing parameters.
It is also important to say that functions can provide scopes. Because in JavaScript, two pairs of curly braces do not provide a local scope as the C program does, whereas in JavaScript only functions can provide similar functionality. For example, a variable declared with Var inside a function is a local variable that cannot be seen externally, similar to the private property in Java. In the curly braces of the global if or for loop, variables declared with VAR are global variables.
2. Terminology relating to functions
JavaScript is a powerful function, so it is necessary to give a uniform terminology to the way it is used. For example, the following code shows a "well-known function expression" (named)

function expression)://named function Expressionvar add = function Add (A, b) {    return a + b;};

If the above code omits the function's name add (that is, the second add), it becomes an " anonymous function expression" (unnamed function expression), also known as an "anonymous" (anonymous functions).

function expression, a.k.a. anonymous Functionvar add = function (A, b) {    return a + b;};

The above two examples can be collectively referred to as " function Expression ", and "famous function expression" is only a special case of the function expression, in fact, the declaration of omitting the name of the function does not affect the definition of the function and subsequent call execution. If the declaration does not omit the name of the function, we can get its name by Add.name, which may be useful when debugging a program.
In addition, there is a "function declaration", which is very similar to a function expression:

function foo () {    //function body goes here}

Syntactically, "function declarations", and "function expressions" are literal declarations of functions (Literal), whose grammatical differences are only in the latter semicolon, and the difference in usage can only be reflected in the actual program. What kind of declarative method should we choose to use? If you are a formal parameter or a method in an object, you can only use a function expression:

This was a function expression,//pased as an argument to the function ' callMe ' callMe (function () {    //I am an Unnam ed function expression    //Also known as an anonymous function}),//This is a named function Expressioncallme (function Me () {    //I am a named function expression    //And my name is "Me"});//another function Expressionvar MyObject = {    say:function () {        //I am a function expression    }};

The difference between a function declaration and a function expression in use is that of a declaration. For example, when a function B is declared in a function A, no matter where the function B is declared, it can be called anywhere within a; Similarly, when an expression of function B is executed in a function A, only the code following the expression can call function B. The following example illustrates the problem:

antipattern//for illustration only//Global functionsfunction foo () {    alert (' Global foo ');} function Bar () {    alert (' Global Bar ');} function Hoistme () {    console.log (typeof foo);//"function"    Console.log (typeof bar);//"Undefined"    foo (); "Local foo"    bar ();//Typeerror:bar is not a function    //function declaration:    //variable ' foo ' and its I Mplementation both get hoisted        alert (' local foo ');    }    Function expression:    //Only variable ' bar ' gets hoisted    //not the implementation    var bar = function () {        alert (' local bar ');}    ;} Hoistme ();

3. The Name property of the function
If you are declaring a well-known function, you can use the Name property of the Function object:

function foo () {}//Declarationvar bar = function () {}; Expressionvar baz = function Baz () {}; Named Expressionfoo.name; "Foo" bar.name; "" Baz.name; "Baz"

The Name property is used when debugging a program, or it can be used when recursive calls are in the body of a function.

The declaration of the JavaScript base function

Related Article

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.