Function (Definition)

The action of executing a function object's script source text.


ECMAScript edition - 2

Functions and methods are similar but not the same thing.

Methods provide a function-like behavior because they can be called and sometimes return a value. They operate on the content of the receiving object to which they are attached and yield information about the internals of the object.

Functions perform some transformation on the input arguments and return a result that is dependent on them. This computation is complete independent of the contents of any object.

Accessor methods to set and get internal values of an object are presented externally as properties.

In JavaScript, functions are first class data items. That means they are implemented as objects and can be manipulated in expressions and assigned to LValues. You could create an array that contains a collection of function objects. You can pass a function as a parameter to another function or store a reference to it in a variable. To do this in a compiled language would require you to do some pointer manipulation. In Objective C, this kind of functionality is called dynamic late binding and allows methods to be attached to event triggers at run-time.

A function is a construct that performs a task. It is derived from the procedural language model rather than the Object Oriented Language model. Procedural language functions are analogous to Object Oriented language methods but they are not quite the same. Functions are called and carry out some procedural task, while methods are invoked by sending a message to a receiving object - they may however belong to a class or an object instance of a class. Class methods are most likely to be factory methods for creating instance objects of that class.

Function calls can only be received by function objects otherwise a runtime error results - however, those Function objects generally belong to other objects and are stored as properties of the owner object.

A method is a specially written function that operates on the receiving object and can be shared between several objects using the 'this' variable to refer to the receiving object.

Associating a function with an object renders that function the active code to be called when a method is invoked on that object. Invoking a method sends a message to the object, the receiving object looks for a property with the message name which contains a reference to callable function object. If it doesn't have one, it calls its parent and delegates the message upwards.

In JavaScript version 1.2, function definitions can be nested within one another. You can end up with local scope chains being nested within one another. Be sure to scope any variables to exist within the level of function nesting that you need. You can accomplish this with the var keyword.

Example code:

   // Example of JavaScript version  1.2 function nesting
   // Create a globally scoped value
   var scope = "Global"
   // Define a function object and store a global reference to it
   myFunc1 = function outer()
      var scope = "Outer"
      // Define an inner anonymous function and store a global reference to it
      myFunc2 = function ()
         var scope = "Inner"
         return scope;
      return scope;
   // Call the various functions to establish the scope of the variables
   document.write("<TABLE BORDER=1>");

See also:Argument, Definition, Function call, Function call operator ( ), function( ... ) ..., Integer-value-remainder, Left-Hand-Side expression, Method, Parameter, Property


ECMA 262 edition 2 - section - 11.2.3

ECMA 262 edition 2 - section - 11.2.4

ECMA 262 edition 2 - section - 13

ECMA 262 edition 3 - section - 11.2.3

ECMA 262 edition 3 - section - 11.2.4

ECMA 262 edition 3 - section - 13