Just JavaScript - The Function Object
Written by Ian Elliot   
Monday, 21 May 2018
Article Index
Just JavaScript - The Function Object
Other Ways Of Creating Function Objects
Constructor v Expression v Statement

The Function Statement

In 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:

function functionname(parameters){
function body
}

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:

var myFunction1=function myFunction2(){};

where both myFunction1 and myFunction2 are variables that reference the same function object, but myFunction2 is local to the Function object – see later.

Hoisting

Final version in book.

Function Objects Are Anonymous

Final version in book 

Constructor v Expression v Statement

Apart 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 Properties

Final version in book

Function Self Reference – callee

Final version in book.

Function Self Reference – Named Function Expressions

Final version in book.

Function Self Reference – Advanced

Final version in book

Summary

  • In JavaScript functions are objects created by the Function constructor.

  • The Function constructor takes a String as the function’s code body and this can be dynamically determined at runtime. This makes optimizations difficult to apply.

  • You can evaluate a function using the invocation operator ().

  • Function bodies can contain local variables which only exist while the function is executing.

  • Functions do not have parameters specified in their declaration and can be called with as many arguments as are required.

  • As parameters are not typed or fixed in number, there is no function overloading in JavaScript.

  • Functions can return an object of any type as a result.

  • As well as the Function constructor, you can also create Function objects using a function expression, arrow function or function statement.

  • Function statements are subject to hoisting where the definition is moved to the start of the function’s scope.

  • Functions, no matter how created, are anonymous and only have variables that reference them.

  • As functions are objects, they can have properties and the code associated with the function can reference these properties using either callee, named function expressions, or a via a private variable created by closure.

 

This is an extract from the book Just JavaScript by Ian Elliot.

Buy Now: from your local Amazon

Just JavaScript 
An Idiomatic Approach

JustJavaScriptSmall

A Radical Look At JavaScript

 

Most books on JavaScript either compare it to the better known class based languages such as Java or C++ and even go on to show you how to make it look like the one of these.

Just JavaScript is an experiment in telling JavaScript's story "just as it is" without trying to apologise for its lack of class or some other feature. The broad features of the story are very clear but some of the small details may need working out along the way - hence the use of the term "experiment". Read on, but don't assume that you are just reading an account of Java, C++ or C# translated to JavaScript - you need to think about things in a new way. 

Just JavaScript is a radical look at the language without apologies.

Contents

  1. JavaScript – Essentially Different
  2. In The Beginning Was The Object
  3. Real World Objects 
  4. The Function Object
          Extract - The Function Object
          Extract - Function Object Self Reference
  5. The Object Expression
  6. Function Scope, Lifetime & Closure
    Extract Scope, Lifetime & Closure
    Extract Execution Context ***NEW!
  7. Parameters, Returns and Destructuring
         Extract - Parameters, and Destructuring
  8. How Functions Become Methods
  9. Object Construction
         Extract: - Object Factories
  10. The Prototype
         Extract - ES2015 Class and Extends
  11. Inheritance and Type
  12. The Search For Type
  13. Property Checking

Buy Now: from your local Amazon

Also by Ian Elliot 
JavaScript Async: Events, Callbacks, Promises and Async Await
Just jQuery: The Core UI 
Just jQuery: Events, Async & AJAX  

<ASIN:1871962579>

<ASIN:1871962560>

<ASIN:1871962501>

<ASIN:1871962528>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 21 May 2018 )