JavaScript basic-JS function

Source: Internet
Author: User

First, how to create a function

1) Normal way

function cal (NUM1, num2) {    return num1+num2;   }

2) Use of variable initialization method

var function (NUM1, num2) {    + num2);} //  Plus (15,20);

3) using constructors

// This method is deprecated, and this syntax causes parsing of two code var New Function ("Num1", "num2", "Return num1+num2") console.log (Box (1,3)); Console.log (typeof box);    //

4) passed as the return value of the function

function Fun (num) {    return num+10;} function fun1 (fun, num) {    return fun + num;} var result = Fun1 (Fun (Ten), ten);    // fun2 (10) The return value of the function is passed here, as is the normal variable console.log (result);

5) passing the function itself

function fun2 ("Fun, num"    ) {return fun (num);} var result = Fun2 (fun, ten); Console.log (Result)// Prints the result variable, which is the function Console.log ( Result ())// call function and print return results

Second, anonymous function

1) General function

function box () {     console.log ("1");}

2) anonymous function

A separate anonymous function cannot be run and cannot be called even if it can be run.

function () {      return "Lee";}

3) assigning anonymous functions to variables

var function () {    return "box: anonymous function";   }

4) Self-invocation of anonymous functions

(function() {    console.log ("1");}) ( );

Or

(function() {    console.log ("2");} ( ));

5) Assigning a value to a variable from a call to an anonymous function

// If there is no subsequent (), the function will be printed out var test = (function() {    return "test: anonymous function";   }) ( );               Console.log (test); Console.log ((function() {return "Print after self-execution"}));

6) parameter of anonymous function

(function(age) {    console.log (age);      // }) (25);

7) anonymous function in function

function Box2 () {    returnfunction () {        //(function inside the closure-function)        return "Anonymous function in function";       }} Console.log (Box2 ());

Or

var b=Box2 (); Console.log (b ());

Three, closed package

Concept: Closures are functions that can read other functions ' internal variables
Usage: 1. You can read the variables inside the function; 2. Keep the values of these variables in memory
Pros: You can place local variables in memory and avoid using global variables (disadvantages of using global variables: large pollution)
Disadvantage: Because the local variable returned by the scope in the closure is not destroyed immediately, it may consume more memory. Excessive use of closures can result in degraded performance. Memory leaks can be caused in IE. The workaround is to remove all unused local variables before exiting the function.

1) Return local variables via closures

function box () {;     var age = +;     return function () {        return age ;    };} Console.log (Box ( ) ());

Example 1: Using global variables to accumulate

var num = +; function Test () {    num+ +;   } Console.log (num);     // execution First: 101 test (); Console.log (num);     // Execute the second time, 102.

Example 2: Accumulating with local variables (can only be performed once)

function test2 () {    var i = +;    I++    ; return i;} Console.log (Test2 ());     // executed for the first time, 101Console.log (Test2 ());    // Execute the second time, 101.

2) Use anonymous functions to implement local variables residing in memory, thus accumulating

functiontest3 () {vara=100; return function() {a++; returnA; };}//Error calling Method:Console.log (Test3 ());//first time, 101.Console.log (Test3 ());//Execute the second time, 101.//To call the method correctly:vart =test3 (); Console.log (t ()); //first time, 101.Console.log (t ());//Execute the second time, 102.t =NULL;//dereference, Waiting for garbage collection (destroy)Console.log (t ());//throws an exception

The closure changes the value of the inner variable of the parent function outside the parent function. So, if you use the parent function as an object, take the closure as its common method, and take the internal variable as its private property (private value), then be careful not to casually

JavaScript basic-JS function

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.