JavaScript Functions 1: function declaration

Source: Internet
Author: User
Next we will introduce a very common ECMAScript object-function (function

The following describes a very common ECMAScript object-function ), we will explain in detail how various types of functions affect context variable objects and what each function's scope chain contains, and answer questions such as the following: what are the differences between the functions declared below? (If so, what is the difference ).

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

Usual method:

function foo() {  ...}

Or, why should the following functions be enclosed in parentheses?

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

But we still need to look at them one by one, starting with the function type:

There are three function types in ECMAScript: function declaration, function expression, and function created by the function constructor. Each type 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, variable objects 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 stage (because FD already exists in VO at the context stage-before code execution ).

For example, a function is called before it is declared)

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

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

// The function can be declared in the following places: // 1) function globalFD () {// 2) in the global context or function innerFD () in the function body of a function () {}}

Only the two positions can declare the function, that is, it cannot be defined in the expression position or a code block.

Additional reading

The topic list of this article is as follows:

  1. How should we understand the working principle of the JavaScript engine?
  2. JavaScript exploration: the importance of writing maintainable code
  3. JavaScript exploration: exercise caution when using global variables
  4. JavaScript exploration: var pre-parsing and side effects
  5. JavaScript exploration: for Loop (for Loops)
  6. JavaScript exploration: for-in loop (for-in Loops)
  7. Exploring JavaScript: Prototypes is too powerful
  8. JavaScript: eval () is the devil"
  9. JavaScript exploration: Using parseInt () for Numerical Conversion
  10. Exploring JavaScript: Basic coding specifications
  11. JavaScript exploration: function declaration and function expression
  12. JavaScript exploration: Name function expressions
  13. JavaScript: function name in the debugger
  14. JavaScript: JScript Bug
  15. JavaScript exploration: Memory Management of JScript
  16. Exploring JavaScript: SpiderMonkey's quirks
  17. JavaScript exploration: an alternative solution to naming function expressions
  18. JavaScript exploration: Object
  19. JavaScript exploration: Prototype chain
  20. JavaScript exploration: Constructor
  21. JavaScript probing: executable context Stack
  22. Execution context 1: Variable object and activity object
  23. Execution context 2: Scope chain Scope Chains
  24. Execution context 3: Closure Closures
  25. Execution context 4: This pointer
  26. Exploring JavaScript: Powerful prototype and prototype chain
  27. JavaScript Functions 1: function declaration
  28. JavaScript function 2: function expressions
  29. JavaScript function 3: function expressions in a group
  30. JavaScript function 4: function Constructor
  31. JavaScript variable object 1: VO Declaration
  32. JavaScript variable object 2: VO in different execution contexts
  33. JavaScript variable object 3: two stages of execution Context
  34. JavaScript variable object IV: Variables
  35. Property of the JavaScript variable object __parent _
  36. JavaScript scope chain 1: Scope chain Definition
  37. JavaScript scope chain 2: function Lifecycle
  38. JavaScript scope chain 3: Scope chain features
  39. JavaScript closure 1: Introduction to closures
  40. JavaScript closure 2: Implementation of closure
  41. JavaScript closure 3: Closure usage

Address of this article: http://www.nowamagic.net/librarys/veda/detail/1662,welcome.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.