O'Reilly logo

Object-Oriented JavaScript - Third Edition by Stoyan Stefanov, Ved Antani

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functions are data

Functions in JavaScript are actually data. This is an important concept that we'll need later on. This means that you can create a function and assign it to a variable, as follows:

    var f = function () { 
      return 1; 
    }; 

This way of defining a function is sometimes referred to as function literal notation.

The function () { return 1;} part is a function expression. A function expression can optionally have a name, in which case it becomes a named function expression (NFE). So, this is also allowed, although rarely seen in practice (and causes IE to mistakenly create two variables in the enclosing scope-f and myFunc):

    var f = function myFunc() { 
      return 1; 
    }; 

As you can see, there's no difference between a named function expression ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required