IIFE versus blocks
As ES5 did not provide block scope, a popular pattern to achieve block scope was to use immediately invoked function expressions (IIFE), for example:
(function () { var block_scoped=0; }()); console.log(block_scoped); //reference error
With ES6's support for block scopes, you can simply use a let
or const
declaration.
Get Object-Oriented JavaScript - Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.