Skip to Main Content
JavaScript and Open Data
book

JavaScript and Open Data

by Robert Jeansoulin
August 2018
Intermediate to advanced content levelIntermediate to advanced
272 pages
5h 3m
English
Wiley-ISTE
Content preview from JavaScript and Open Data

6Functions

A function is an independent block of code (instructions list), which can be called from another part of the code. This requires a mechanism for storing the calling line, sharing variables, passing parameters and returning a value.

In JavaScript, a function is a “first-class object”, which means that a function can:

  • – be assigned to a variable;
  • – be an argument to other functions;
  • – be returned as a value;
  • – have properties, methods and a prototype, like every other object.

To illustrate this notion, we present a “prototypal diagram”, as in Chapter 5, which shows how intricate “Object” and “Function” are:

 Object.constructor =             		  Function
 Object [[prototype]] ===                         Function.prototype
 Object.prototype.constructor =           	  Object
 Object.prototype [[prototype]] ===               null                  // alone
 Function.constructor =                      	  Function              // itself
 Function [[prototype]] ===                       Function.prototype
 Function.prototype.constructor =                 Function
 Function.prototype [[prototype]] ===             Object.prototype

This chapter presents the notions of “scope” and “execution context”, which will shed light upon the mechanisms of “function closure” and “IIFE” (immediately invoked function expression).

The diagram below summarizes what will be detailed, and where the notions of free variables and bound variables will be used.

image

6.1. General syntax of a JavaScript function

A function can be set for the same syntax in two ways: by ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Data Wrangling with JavaScript

Data Wrangling with JavaScript

Ashley Davis
Web Applications with Javascript or Java

Web Applications with Javascript or Java

Gerd Wagner, Mircea Diaconescu

Publisher Resources

ISBN: 9781786302045