Skip to content
subtleGradient edited this page Feb 16, 2011 · 7 revisions

IIFE Leading semicolon

The problem

File1.js

1 + 1 // lol, math

File2.js

// Yay, comments
(function(){ /* Teh Codez */ }())

File1+File2.js

1 + 1 // lol, math
// Yay, comments
(function(){ /* Teh Codez */ }())

Result: TypeError: number is not a function

The solution

;(function(){ /* Teh Codez */ }())

Semicolon FTW

The wrong solution

File1.js

1 + 1; // lol, math

Obviously you could simply add a semicolon to the first file instead of the second. But the problem with that approach is that the people who control File2.js (i.e. the Slick project) have absolutely no control over File1.js. It may be ugly and not ideal, but adding a single harmless character to File2.js solves a potentially difficult to debug issue.

Clone this wiki locally