Skip to Main Content
Hands-On Functional Programming with TypeScript
book

Hands-On Functional Programming with TypeScript

by Remo H. Jansen
January 2019
Beginner content levelBeginner
210 pages
4h 47m
English
Packt Publishing
Content preview from Hands-On Functional Programming with TypeScript

Immediately-invoked functions

An immediately-invoked function expression (IIFE) is a design pattern that produces a lexical scope using function scoping. IIFE can be used to avoid variable hoisting from within blocks or to prevent us from polluting the global scope, for example:

let bar = 0; // global(function() {    let foo: number = 0; // In scope of this function    bar = 1; // Access global scope    console.log(bar); // 1    console.log(foo); // 0})();console.log(bar); // 1console.log(foo); // Error

In the preceding example, we have wrapped the declaration of a variable (foo) with an IIFE. The foo variable is scoped to the IIFE function and is not available in the global scope, which explains the error when trying to access it on the last line.

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

Functional Programming in JavaScript

Functional Programming in JavaScript

Luis Atencio
TypeScript Quickly

TypeScript Quickly

Yakov Fain, Anton Moiseev
Functional Programming in Kotlin

Functional Programming in Kotlin

Runar Bjarnason, Paul Chiusano, Marco Vermeulen

Publisher Resources

ISBN: 9781788831437Supplemental Content