Detailed analysis of JavaScript function definitions and javascript function definitions

Source: Internet
Author: User
Tags variable scope

Detailed analysis of JavaScript function definitions and javascript function definitions

Function

Key points:

A). functions are first-class citizens (importance) in javascript)
B). A function is an object.
C). The function defines an independent variable scope.

Definition Method

A) Name function:

Unless defined in another function, the name function is global.

// Global name function add (x, y) {return x + y;} console.info (add (100,200); // 300

B) Anonymous functions:

An anonymous function is usually assigned to a variable and called through the variable.

    var func = function (x, y) {      return x + y;    }    console.info(func(5, 2)); //7

Anonymous functions are applicable to the following "anonymous functions executed immediately:

Console.info (function (x, y) {return x + y;} (100,200) // call now );

C) The definition method affects the code execution result.

The name function can be used before being defined.

    console.info(sum(10, 10));    function sum(num1, num2) {      return num1 + num2;    }

The anonymous function must be defined before use.

    //console.info(sumFunc(10, 10));  //Uncaught TypeError: Property 'sumFunc' of object [object Object] is not a function     var sumFunc = function (num1, num2) {      return num1 + num2;    };    console.info(sumFunc(10, 10));

Function return value:

Return Value is generated using return. If no return is returned, the function returns undefined.

Function func () {} console.info (func (); // undefined function func2 () {return; // NULL return statement} console.info (func2 (); // undefined

The pitfalls hidden in return:

 var func = function (x, y) {   var sum = x + y;   return {     value : sum   } }

There is no problem with this writing: the Object {value: 10} returned by calling func (5, 5}

However:

  var func = function (x, y) {    var sum = x + y;    return    {      value: sum    };  }  console.info(func(5,5)); //undefined

If return is followed by a carriage return,
Call func (5, 5) to display undefined
The editor adds a semicolon after return. However, in this case, there is no use.

Functions are objects:

Function add (x, y) {return x + y;} console.info (add (100,200); // 300 var other = add; // other and add reference the same function object console.info (other (300,400); // 700 console.info (typeof other); // function console.info (add = other ); // true

Nested defined functions:

Within a function, you can define another function.

  function outerFunc(a, b) {    function innerFunc(x) {      return x * x;    }    return Math.sqrt(innerFunc(a) + innerFunc(b));  }  console.info(outerFunc(3, 4)); //5

Access external variables:

Internal functions can access external variables and parameters.

 var globalStr = 'globalStr'; function outerFunc2(argu) {   var localVar = 100;   function innerFunc2() {     localVar++;     console.info(argu + ":" + localVar + ":" + globalStr);   }   innerFunc2(); //hello:101:globalStr } outerFunc2("hello");

Function of the returned function:

Because the function is an object, it can be used as the return value.

  function outerFunc(x) {    var y = 100;    return function innerFunc() {      console.info(x + y);    }  }  outerFunc(10)(); //110

The above is all the content of this article. I hope you will like it.

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.