JavaScript Self-Executing Anonymous Functions

This post demonstrates how to create SEAFs (Self-Executing Functions) in JavaScript and briefly shows how AngularJS applications heavily use this type of function.

Software Environment

  • Windows 10
  • Notepad or Notepad++
  • AngularJS 1.6.x
  • Plunker – https://plnkr.co

Self-Executing Anonymous Functions

An AngularJS application uses a lot of SEAFs (Self-Executing Functions).  When you reference an .js file in your main .html, most likely your .js codes are enclosed within a SEAF.

What are SEAFs then? These are functions that are defined and executed at the same time (when referenced). All codes withing an SEAF are then executed.

Defining and Executing an SEAF

An SEAF is a JavaScript code with the following syntax

Take these referenced .js files that are part of  an AngularJS project.

These files have codes within  SEAFs.

app.config.js

app.controller.js

Plain SEAF JavaScript

You may check out the sample created at Plunker – https://plnkr.co/nYWCkeuttrPjRlL8sy4x

 

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert