JavaScript Note 4-function expressions

Source: Internet
Author: User

  1. The general form of the creation function, the function declaration is read before executing the code, so you can write the function declaration below the function call:
    Sayhi (); function Sayhi () {         alert ("hi!");}

  2. To create a function using a function expression, you must first assign a value before calling:
    Sayhi ();  Error!! function does not exist var sayhi=function () {         alert ("hi!");}

  3. Recursion.
    • General recursion
      function factorial (num) {                if (num <= 1) {                    return 1;                } else {                    return num * factorial (num-1);                }            }

    • Arguments.callee pointer to the function being executed, which can be used to implement recursion:
      function factorial (num) {                if (num <= 1) {                    return 1;                } else {                    return num * Arguments.callee (NUM-1); c15/>}            }

  4. Closed Package(Closure refers to a function that can access variables in another scope).
    • a common way to create closures: Create another function inside one function. When the function executes, an execution environment and the corresponding scope chain are created. Closures can only get the last value of any variable in the containing function:
       function createfunctions () {var result =                                New Array ();                    for (var i=0; i <; i++) {Result[i] = function () {return i;                };            } return result;                        } var Funcs = Createfunctions ();  Every function outputs ten for (Var i=0; i < funcs.length; i++) {document.write (Funcs[i] () +            "<br/>"); } 

      All of the above code output is 10. This is because each Funcs function holds the createfunctions () object (which is a function, an object, and a reference type function type), and Createfunctions () has a variable i, so, Each funcs will have this variable i, and the function of Createfunctions () when returning the result, I has become 10. So each value of the Funcs array is ten.

    • Can be transformed as follows:
      function Createfunctions () {                var result = new Array ();                                for (var i=0; i <; i++) {                    Result[i] = function (num) {                        return function () {                            return num;                        };                    } (i);                }                                return result;            }

      When each anonymous function is called, the current value of I is given to NUM, and the closure of NUM is created and returned inside the anonymous function. In this way, each function that returns an array has a copy of its own num variable. (This paragraph is not clear, the reader himself again, if there is a better way to describe, please comment under the article, thank you)

  5. This object
    • In the global function, this is equivalent to window.
    • When a function is called as a method, this is equivalent to that object.
    • When each function is called, the function automatically acquires two special variables: this, arguments. The intrinsic function searches for these two variables only until the active object is searched.
  6. Impersonation Block-level scope (private scope)
    • As follows:
      function Outputnumbers (count) {for                (var i=0; i < count; i++) {                    alert (i);                }                            alert (i);   Count            }            outputnumbers (5);

      In languages such as Java, the variable I in for is destroyed when it is exhausted. In JavaScript, Outputnumbers generates the active object when called, and this I belongs to the active object, so since it is defined, it can be accessed anywhere within the function, which is common within the active object.

    • Syntax for anonymous functions (creating private scopes):
      (function () {  //This is block-level scope});

      A function declaration is placed in parentheses to indicate that it is an expression, and that it can be called immediately after a parenthesis is added.

    • If you need some variables temporarily, you can use a private scope:
       function Outputnumbers (count) {                    (function () {for (Var i=0; i < count; i++) {alert (i);                                }                })();   alert (i); Causes an error} 

      In the above code, I is private and an error is made when accessing I outside of the anonymous function (private domain), even though alert is still Within the active object.

  7. Private variables
    • The parameters of the function, local variables, and other functions defined inside the function belong to the private variables of the function. Like what:
      function Add (num1,num2) {    var sum = num1 + num2;    return sum;  }

      There are 3 private variables: num1,num2,sum. They can be accessed inside the function, but not externally.

    • Privileged methods can access private variables: Simply put, use an expression to give it a closure, and access other functions inside the closure:
      function person (name) {

      var a=0; This.getname = function () { return name + A; }; This.setname = function (value) { name = value; }; }
      This.getname, This.setname is the expression, after the person instance is created, the name, a property can only be accessed through GetName or SetName.

JavaScript Note 4-function expressions

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.