JavaScript Advanced Programming: Chapter Seventh-Functions

Source: Internet
Author: User

vi. function Expressions
  You can use anonymous functions when you use a function as a value
  1. Recursive
    A recursive function is made up of a function that calls itself by name///using a function declaration to define a recursive function may be problematic//This is a classic recursive factorial function factorial (num) {  if (num<1 ) {    return 1;  } else{    return num * factorial (num-1);  }} When a function name is used to define the recursive function, the function names and functions are coupled. When the other pointer points to the body of the function, the execution of ANOTHERFAC () must execute factorial, and the factorial variable is set to NULL, which causes the error var ANOTHERFAC = factorial; factorial = null; ANOTHERFAC (5);//You can use Arguments.callee to solve the problem, do not repeat//can also assign a named function expression to the variable factorial, This solution still works in strict mode       
  2. Closed Package

    A closure is a function that has access to a variable in another function's scope (the most common manifestation is to return another function from one function), which is easy to confuse with anonymous functions. When a function is called, an execution environment and the corresponding scope chain are created. The active object initialized by the value of the function's arguments and other named parameters is ranked first in the scope chain, the active object of the outer function is always second, and the outermost is the global variable object . In general, when the function is finished, the inactive object is destroyed, only the global variable object is saved in memory, but the case of the closure is different. Closures carry a function scope that contains it, consuming more memory than other functions. So be careful with closures. the This of all anonymous functions points to window. JavaScript does not have a block-level scope concept, so the scope of the active variable defined in the statement block expands to the entire function body. You can use anonymous functions (function () {}) () to avoid this situation. The problem with the explanation (function () {}) () is on page 185. Private variables can help to understand why an object's constructor looks like that. initializing undeclared variables always creates a global variable. (This is also the difference between a function declaration and a function expression, where a function declaration can only create a local function )

JavaScript Advanced Programming: Chapter Seventh-Functions

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.