Javascript Jems - First class functions
Tuesday, 10 August 2010
Article Index
Javascript Jems - First class functions
Pass by value
Clone functions

Banner

In Javascript the function occupies a rather unusual role – it’s a first class object. That is a Javascript function is an instance of a Function object. This much is fairly well know but it’s also a first class object that differs from more basic objects. Let's take a look at some interesting ways that functions and objects work together.

Function basics

There are, at least, three distinct ways of creating a function and this does nothing to make understanding easy.

The first way is to use the function statement.  This is the most commonly used way of creating a function and the one that beginners are generally taught (and should be taught first):

function myfunc1(a)
{
return a + 1;
};

In this case myfunc1 is a “function name” and not a simple variable.

The second is to use the function operator:

var myfunc2 = function(a)
{
return a + 1;
};

This form is often used to construct anonymous functions, by not assigning the result to a variable, and often encountered as part of object-oriented Javascript. Notice that in this case myfunc2 is just a standard variable following the usual rules of variable use, scope and lifetime.

The third and final way is to use the Function object's constructor. Every Javascript object has a constructor and Function constructs a function object:

var myfunc3 = new Function(
"a", " return a + 1;");

That is, the Function constructor accepts a variable number of string parameters – the last of which is the function body and the remainder are functions. Also notice that Function is always treated as if it had been written as new Function, i.e. it always creates a new object.

All three ways create a function that you can call in the same way, e.g. myfunc1(34) etc. From this point of view they are identical.

However, the first creates a function variable and the second just a standard variable that references a function. You can use the second form to create a function variable as well if you want to:

var myfunc2 = function myfunc4(a)
{
return a + 1;
};

Now you can call the same function using myfunc2 or myfunc4. You can think of the function operator as being used in both cases such that it defines a function variable if one is provided and a standard reference variable if one is used.

The big difference is between the first two and the third. This creates a function object on the fly in the sense that its parameters are strings that might well be constructed at run time rather than compile time.

The function operator/statement can’t be used in this way and so it is possible for the JavaScript engine to perform optimisation at “compile” time (although you need to keep in mind that JavaScript is an interpreted language). 

In this sense the Function constructor is a lot like the Eval function which will evaluate any JavaScript commands stored in a string.

If you are worried about efficiency don’t use the Function constructor.

Functions as object

Functions, no matter how they are defined are objects. You can treat them as objects without exception. For example if you you want to add a property to a function then you can:

myfunction.argcount=1;
alert(myfunction.argcount);
function myfunction(a)
{
return a+1;
}

In this case we have added the argcount property to the function object myfunction and displayed its value.

What might seem even stranger is that a function object can have methods. Yes that's correct a function can have functions. For example, the getargcount method works as you would expect:

myfunction.getargcount=function()
{return myfunction.argcount;};
alert(myfunction.getargcount());

The Function object has a range of built in properties and methods. For example, the toString method returns the source of a function as a string:

alert(myfunction.toString());

There is a length property that returns the number of arguments in the function.

alert(myfunction.length);

Also notice that there are some properties that only make sense when used while the function is being evaluated. For example, the arguments property gives an object which contains the arguments actually used in a call. If you try and make use of this property outside of the function then it is null - which is reasonable.

Banner

<ASIN:0470684143>

<ASIN:0137054890>

<ASIN:0470526912>

<ASIN:1449381871>

<ASIN:1430230541>

<ASIN:0321683919>



Last Updated ( Thursday, 19 August 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.