On the _javascript skill of javascript function expression

Source: Internet
Author: User
Tags anonymous closure

Start learning JavaScript function expressions and read the following carefully.

1, the general form of the creation function, before executing the code will read the function declaration, so you can write the function declaration under the function call:

 Sayhi ();
 function Sayhi () {
     alert ("hi!");
}

2. Use function expressions to create functions, which must be assigned before they are called:

 Sayhi (); Error!! function does not exist
 var sayhi=function () {
     alert ("hi!");
}

3, recursive

General recursion

function factorial (num) {
        if (num <= 1) {return
          1;
        } else {return
          num * factorial (num-1);
        }
      }

Arguments.callee A pointer to the function you are executing, which you can use to implement recursion:

function factorial (num) {
        if (num <= 1) {return
          1;
        } else {return
          num * Arguments.callee (NUM-1); c26/>}
      }

4, Closure ( closure refers to a function, this function can access the variables in another scope ).
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. The closure can only get the last value of any variable in the 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 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 active object (which is a function, also an object, and a reference type function type), and Createfunctions () has a variable i, so, Each funcs will have this variable i, and createfunctions () This function returns the result, I has become 10. So each value of the Funcs array is 10.

It can be modified 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 invoked, the current value of I is given to num, while inside the anonymous function, the closure of NUM is created and returned. Thus, each function that returns an array has its own copy of the NUM variable. (This paragraph is not clear, the reader himself again, if there is a better description method, please comment under the article, thank you)

5. This object

    • Global function, this is the window.
    • When a function is called as a method, this is the equivalent of that object.
    • When each function is invoked, this function will automatically fetch two special variables: this, arguments. The intrinsic function searches for both variables only until the active object is searched.

6, imitate 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 I run out of it. In JavaScript, the active object is generated when the outputnumbers is invoked, and this I belongs to the active object, so that it can be accessed anywhere within the function from the time it is defined, and it is common within the active object.

Syntax for anonymous functions (Create private scope):

(function () {
 //here is block-level scope
}) ();

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

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 code above, I is private, and an error occurs when I access an anonymous function (private domain) outside, although alert is still in the active object.

7. Private variables
The parameters of the function, local variables, and other functions defined within the function belong to the private variable 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 within 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 an expression, and after you create a person instance, you can access the name, a property only through GetName or SetName.

The above is the entire content of this article, I hope that the learning of everyone to help.

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.