Kyle Pennell Kyle Pennell - 1 year ago 124
Javascript Question

Will const and let make the IIFE pattern unnecessary?

As I understand it, the IIFE pattern is a work around to the fact that ES5 and below do not have a way to create block scopes. By wrapping everything in a function and immediately invoking it, we can create a scope.

Now that

will gain support by more the browsers, does this reduce the need for something like the IIFE pattern?

Answer Source

Yes, blocks are going to replace IEFEs, as soon as block-scoped declarations (functions, let/const/class) become widely adopted. You need a scope, e.g. for a closure? Here you have a block, be it a loop body or just part of a statement list.

However, there is still one application of IEFEs that blocks cannot replace: the module pattern. Blocks don't have return values, and mutating higher-scoped variables is ugly, so we will still see function expressions in the creation of objects that need private state:

const example = (() => {
    return …;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download