In-depth analysis of ECMA-262-3: Chapter 5 Functions

Source: Internet
Author: User
Tags types of functions

Introduction

In this article, we will discuss in more detail a common ECMAScript object-function. We will study in detail the various types of functions and explain how these types of functions affect a context variable object and what is included in the scope chain of a function of a certain type. We will answer questions frequently asked in the Forum: "What is the difference between the function created below and the function defined in a" habitual way "(if any, what is it ?)" (Note: I have adjusted the order of the original text)

var foo = function () {  ...};

Functions defined in a "habitual way:

function foo() {  ...}

Or, "Why should the following function calls be surrounded by parentheses ?"

(function  () {  ...})();

Because these articles are independent from earlier chapters, and because we will actively use the syntax from these chapters, to fully understand this part, it is worth reading chapter 2 if necessary: variable object and Chapter 4: Scope chain.

But let's proceed one by one, starting with studying the function type.

Function Type

There are three function types in ECMAScript, each of which has its own characteristics.

Function Declaration

Function declaration (FD) is a function:

  1. There is a specific name
  2. Position in the source code: either in the Program level or in the FunctionBody of other functions
  3. Create in the context stage
  4. Variable objects
  5. The following statement
function exampleFunc() {  ...}

The main feature of this function type is that they only affect variable objects (that is, the function is stored in the context VO ). This feature also explains the second important point (it is the result of the properties of the variable object)-they are available in the code execution phase (because FD has been collected by VO In the context phase ).

For example, (functions in the source code are called before they are declared)

foo();function foo() {  alert('foo');}

Another important point is the second point in the definition-the position of the function declaration in the source code:

// function declaration// is directly in:// either the global context// at Program levelfunction globalFD() {  // or directly inside the body  // of another function  function innerFD() {}}

Function declaration cannot appear anywhere in the code -- that is, it cannot be defined in the expression position or a code block.

Instead of the function declaration (more likely to be considered opposite to it), it is a function expression.

  • 7 pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • Next Page

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.