Page 1 of 3
Buy Now: from your local Amazon
An Idiomatic Approach
- In The Beginning Was The Object
- Real World Objects
- The Function Object
Extract - The Function Object
Extract - Function Object Self Reference
- The Object Expression
- Function Scope, Lifetime & Closure
Extract Scope, Lifetime & Closure
Extract Execution Context ***NEW!
- Parameters, Returns and Destructuring
Extract - Parameters, and Destructuring
- How Functions Become Methods
- Object Construction
Extract: - Object Factories
- The Prototype
Extract - ES2015 Class and Extends
- Inheritance and Type
- The Search For Type
- Property Checking
Buy Now: from your local Amazon
Also by Ian Elliot
Just jQuery: The Core UI
Just jQuery: Events, Async & AJAX
In this chapter we meet the most important object of all – the Function object. What really matters is that you don't think that it is just a function.
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, and that is all there is to the program.
So when 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 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 also 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.
Constructing Function Objects
A Function object is created in the way any built-in object is, via a suitable constructor, and used in the way all objects can be used.
To create a new Function object you use:
var myFunction= new Function();
The use of new is optional.
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 an object like any other".
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 function’s "body".
var myFunction= new Function(
"alert('hello function body');");
This creates a Function object with a body that consists of the single statement:
alert('hello function body');
So a Function object has some code stored within it.
This code is immutable after the Function object has been created – you can't modify it. If you want to change the code you have to create a new Function object.
The code may be immutable but it is specified as a String and this means that it can be the result of a String expression which computes the code at runtime. It is this possibility that makes it impossible to optimize this way of specifying a Function.
All you can do with the function body is execute the code it by using the function invocation 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 invocation operator, is a variable that references the Function object and:
i.e. with the function invocation operator, evaluates the body of the Function object that myFunction references.
The code in the function body can declare local variables and these are not properties of any object. Local variables only come into existence when the function is executed and they are destroyed i.e. removed from memory when the function finishes. They are also private to the function and cannot be accessed by any other code. They can be accessed by functions that are defined within the function concerned and this is something we need to consider in more detail later.
To create a local variable you have to use the var keyword:
This creates a local variable which only exists while the function is executing. The unexpected detail is that due to hoisting, see later, local variables exist for the entire lifetime of the function irrespective of where they are declared.
If you don’t use the var keyword then the variable is interpreted as a property of the global object. For example:
is the same as:
As explained earlier, properties of the global object behave like global variables in other languages.
One side effect of using properties rather than global variables is that when you write:
then if the property doesn’t exist it will be created. What this means in practice is that forgetting to use var in a function adds properties to the global object that you probably never intended to add. Most IDEs and smart editors will warn you if you don’t use var in a function. In practice it is better to always use this to access or create a property or var to create a variable.
In strict mode you have to use var in a variable declaration. If you don’t then a runtime exception occurs. Also in strict mode the global object ‘this’ inside a function is undefined. This means a function cannot easily create a global variable or access the global object. Notice that this doesn’t stop a function from using a global variable, only creating a new one.