Just JavaScript - The Function Object |
Written by Ian Elliot | ||||
Monday, 21 May 2018 | ||||
Page 3 of 3
The Function StatementIn an ideal world the account of the Function object would come to an end at this point – you have enough to write any program you care to but not in a style most programmers would recognize. Most languages introduce functions using a statement not an expression. The idea of assigning a reference to a function to a variable is something that many languages have only recently added under the name “lambda functions” or “lambda expressions”, JavaScript has this other way of defining a function – the Function statement. Instead of writing an assignment you simply write:
This results in a Function object with the specified parameters and function body being created – as before. What appears to be new is the functionname. This is handled in a slightly different way by the JavaScript engine but it behaves much like a standard variable and you can assign it a new value. Think of the function statement as declaring the functionname variable. Function names are in fact treated in special ways; in particular the function name is “hoisted”. It is as if the function declaration was positioned at the top of the program or of the function it is declared in so that the function can be used before it has been defined. Hoisting is described in more detail in the next section but the reason that JavaScript implements such a seemingly odd action is that this is how other languages tend to work with respect to functions. In a more traditional language you can write a main program that uses functions that are defined later in the text of the program. JavaScript achieves the same result by using hoisting. Notice that Function objects created using the constructor or an expression are not hoisted and have to be declared before their first use. Function names can be assigned to and generally treated like variables but they are also used by debuggers to report information on functions and how you are using them. Apart from hoisting, a statement function works in the same way as a function expression. If you really want to you can confuse the issue by assigning a function name in a function expression. That is:
where both myFunction1 and myFunction2 are variables that reference the same function object, but myFunction2 is local to the Function object – see later. HoistingFinal version in book. Function Objects Are AnonymousFinal version in book Constructor v Expression v StatementApart from hoisting and some matters of optimization, there isn't much difference between the three different ways of creating a Function object. However, you should use function expression/arrow or function statements in preference to the constructor because the JavaScript engine can optimize these better. Although the function name supplied as part of a function statement looks like an immutable name, as it would be in most other languages, all three ways of creating functions create anonymous Function objects. There is one other important difference between function expressions and statements compared to Function objects created using the constructor. The constructor always creates an object that is in the global scope. That is, it cannot be used to create functions that are local to other functions, whereas function expression and statements can create local functions. More of this is in the next chapter where we look in detail at scope, lifetime and closure. Function PropertiesFinal version in book Function Self Reference – calleeFinal version in book. Function Self Reference – Named Function ExpressionsFinal version in book. Function Self Reference – AdvancedFinal version in book Summary
This is an extract from the book Just JavaScript by Ian Elliot. Buy Now: from your local AmazonJust JavaScript
|
||||
Last Updated ( Monday, 21 May 2018 ) |