|Written by Ian Elliot
|Thursday, 01 May 2014
Page 1 of 3
There is a newer version of the draft of the book here.
The mainstream object oriented languages give in to the very natural pressure to make code special.
You want to write a program - so you want to get on and write some code but where does that code live?
In non-object based languages there is only the code. You start to write a list of statements that this is all there is to the program.
So you first start writing programs you tend to think that the code is the most important thing, but later you learn about objects and you are told that code is just always a property of an object, i.e. the object's methods.
That is, the language has objects and objects have properties which are usually other objects and they have methods which are blocks of executable code.
That is, the answer to the question of where does the code live, in the case of an object based language, is that code exists as methods which aren't objects but a special type of property that an object can have.
Objects are the main entity in the program and can be assigned and passed around. The code is always bound to some object or other and so you can't do things like pass code as a parameter because it isn't an object. This makes tasks such as event handling and callbacks difficult.
So in most object based languages there are objects and there are methods - which are executable properties.
The Function object
A Function object is created in the way any object is and used in the way all objects can be used.
To create a new Function object you use:
Once created you can add properties to the new object in the usual way:
The point that is being made is that the Function object is "just this object".
So what additional characteristics does the Function object have that makes it useful?
The simple answer is that a Function object can accept some code as part of its creation.
When you create a Function object you can specify a list of statements that form the functions "body".
This creates a Function object with a body that consists of the single statement
So a Function object has some code stored within it.
This code is immutable after the Function object has been created - you you can't modify it. If you want to change the code you have to create a new Function object.
All you can do with the function body is execute the code it by using the function evaluation operator ().
causes the list of statements that make up the Function object's body to be executed. In this case it simply causes an alert box to appear with the message.
without the function evaluation operator, is a variable that references the Function object and
i.e. with the function evaluation operator, evaluates the body of the Function object that myFunction references.
You might well be thinking if the evaluation operator evaluates the function it should return a result and this is the case. You can use the statement
to specify what the function evaluates to.
If you don't specify a return result the function body returns undefined.
has the function body
Now if you try:
you will see 3 displayed as this is the result of the function.
The value of the function can be any object not just a Number object (actually a primitive value) as in the example.
The final part of the Function object we need to examine is the use of parameters.
As well as the body of the function you can also specify a list of parameters which you can specify values for when the function is evaluated.
now the function will return the sum of a and b no matter what they are set to when the function is evaluated.
sets a to reference 1 and b to reference 2 and then evaluates the body.
Notice that function parameters can be any object and not just the Number objects used in the example. You can also think of what is passed as being a reference to an object, even it it happens to be a primitive value, in all cases.
You can also use as many parameters in a function definition as you care to define and use as many as you care to when you execute the function. For each parameter you give a value to there is a variable of the same name with that value as part of the function's code. Any missing parameters are undefined and it is up to your code to deal with this.
The Function expression
This said there are times when converting a String object containing code into an executable Function object is useful but these tend to be considered advanced techniques.
This is just a shortcut to creating a Function object and doesn't really introduce anything new but you can now write:
Notice that the function body is now specified as statements enclosed in curly brackets.
For example the previous Function object can be written:
or by including line-breaks:
Which has the huge advantage that this looks like a function definition in a non-object oriented language. Notice also that you don't use
i.e. you don't use the new keyword, so that it doesn't look like the creation of an object and looks even more like a non-object oriented function.
It is important to keep in mind that while this looks like a simple function declaration it isn't. A function expression creates a Function object and you can add properties to it as you like.
Also notice that the variable assigned to becomes a reference to the function object. This means you can do things like
and anotherRef becomes another reference to the same Function object.
It is the Function object that is important not the variable that references it.
Notice also that this means that there is no real concept of "function name" introduced so far - although we will introduce it later.
|Last Updated ( Sunday, 10 May 2015 )