ECMA-262 Core: JavaScript Function Statements

Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site

Introduction And Review

In JavaScript, the term "function" can mean many things. The term "function" is a keyword in the JavaScript language. The function keyword is used in statements to create:

  1. Instance Methods
  2. Class Methods
  3. Function Statements

Instance Methods and Class Methods are methods of objects or methods of a class type and apply to the Object Oriented Programming (OOP) paradigm. JavaScript as it applies to OOP is covered separately in our section on Objects and more specifically at a page entitled, Object Oriented Design and JavaScript.

This leaves us with the function statements. A variation of the function statement is the literal function statement. The slight difference of the function literal is cover at our page entitled, Core JavaScript Function Literals. But be advised that the comments pertaining to the function statements also apply to the function literal unless otherwise stated.

The function statement will be our focus here. Henceforth, instead of the term function statement, we will use the term function statement declaration and refer to it with the acronym "FSD". Our discussion of the FSD will focus on the traditional usage of the term "function". We clarify this terminology at this link.

So, what are the characteristics of a FSD? The following list provides an overview of FSD characteristics.

User Defined Functions

Terminology clarification: when you declare a function you have built a user defined function. Using any of the three declaration approaches gives a user defined function. However, user defined functions are typically built with the FSD. In fact the terms "user defined function" and FSD are nearly synonymous. Be aware that JavaScript also has built-in functions.

The Traditional Function

Sometimes referred to as modules or subroutines or methods, the function has a traditional meaning in the older structured programming paradigm. Functions lend themselves to structured programming principles; a central repository of code that is reusable. The traditional function is defined in one area of your program but may be accessible from most other areas in the same program. The access to the function declaration and its body of code is typically via a statement that invokes the function. The invoking statement passes control (and maybe parameters) to the function and upon the completion of the function's processing, the control (and maybe return values) is passed back to the invoking statement and execution picks up at the next statement following the invoking statement. The invoking statement in JavaScript is the call statement.

To summarize, we have described the traditional function as belonging to the structured programming paradigm. And when we refer to the FSD (function statement declaration), it is in this traditional context that the term best fits.

The Function Statement Declaration

The Function Statement Declaration or FSD is our term for JavaScript functions that are to be declared and used in the traditional programming style (as opposed to the OOD paradigm). The following table gives a comparison of JavaScript functions as implemented via the FSD and those implemented via the Function class constructor. The former being applicable to the traditional structured programming paradigm and the later applicable to the OOD paradigm

Comparison of FSD and Function Class Implementation
Feature FSD Function Class
Program Style
traditional structured programming object oriented programming
function keyword Function Native Class Constructor
functions are named with the declaration statement (FSD) an object instance must be instantiated (thereby named) from a Function class declaration; the actual declaration is anonymous
the named function can behave like an object objects are instantiated from the Function class type
N/A the new operator is used to instantiate an object instance
the call statement invokes the function by name an object instance is accessed with the call statement
one copy of function in memory
dynamic (non-static)
one copy per instance
Parsed once when page is loaded once per access
declaring a function within another function is allowed possible but not applicable
an internal function should take the form of a method (a class method or a prototype instance method)
Scope outer most function has global scope,
inner function has local scope
local or global: depends on where in program the object is instantiated
a temporary object triggered by the function invocation

FSD Statement Components:

function function_name (parm1, parm2, .... , parmx) { ......body (JavaScript statement)..... }

The FSD syntax: in addition to the set sequence given above, some of the components of the FSD declaration statement require a specific syntactical form:

FSD Return Values

When called, a function will always return a value. The designer of the function can control the value returned to the caller with the return statement. Without an explicit return statement specified in the function body, a value of undefined will be returned.


Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson