4

I recently watched a JavaScript video tutorial containing code similar to this one:

(function (){
    var b = 10,
        c = 20, 
        d = 50;

    var e = function(){
        return b + c + d;
    };
    return e();
}());

This is a common JS coding pattern. The author said that this is an example of a procedural approach in Javascript code. I don't understand that, can you please explain.

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
Miroslav Trninic
  • 3,327
  • 4
  • 28
  • 51

1 Answers1

7

Let's start by saying the above is a self-invoking anonymous function:

A self-invoking anonymous function runs automatically/immediately when you create it and has no name, hence called anonymous.

The above is a fairly poor example in my opinion, but consider the following change:

var f = (function (){
    var b = 10,
        c = 20, 
        d = 50;

    var e = function(){
        return b + c + d;
    };
    return e();
}());

​console.log(f);​ 

We've taken the above code and added a var f = in front of the function. This returns the value of e() from the inner function to f and now you have a value f to be used elsewhere. Since the variables b, c, d, e are declared within the function scope of the anonymous function, we can ensure that they will not be tampered with. This is a way to do private variables in JavaScript. Now say you had 10 + 20 + 50 may places within your code. You could run this anonymous function at the beginning and substitute those occurrences with f. This idea of abstracting out code into various procedures is why it's referred to as Procedural Programming.

Procedural programming uses a structured approach to coding the program where the functionality of the program is broken down into a number of functions or subroutines to make it easier to follow and reduce the need to repeat code.

This is mainly used to make code easier to read, easier to follow/debug and lets you abstract commonly used chunks of code into well written snippets of code. 


Read more


Self-Invoking Functions

Purpose of Self-Invoking Functions

Procedural Programming

More Procedural Programming

and this moves you into Closures

Community
  • 1
  • 1
Chase
  • 29,019
  • 1
  • 49
  • 48
  • thanks, I understand self-invoking functions, but why procedural ? is it because it is called automatically (by "procedure")? or it has something with the code inside ? – Miroslav Trninic Nov 02 '12 at 14:06
  • @carousel I bet the speaker was probably very fond of buzzwords such as OOP. Well, not a buzzword anymore from what I can tell, but some people are acting like it still is. – Ionuț G. Stan Nov 02 '12 at 14:12
  • 1
    In my college years when we learned Pascal there were "functions" and "procedures". Both were just a piece of code, but function had to return result and procedure didn't return anything just processed some logic. You use function which actually return nothing, so we can call it procedure. – Vyacheslav Voronchuk Nov 02 '12 at 14:16
  • @carousel: I've added a bit more about why it's considered procedural. Feel free to take a look and let me know if there's anything that's unclear. Thanks – Chase Nov 02 '12 at 14:33
  • thank you Chase - I see it is not just about functions (procedures). but also about a scope in which variables are declared. – Miroslav Trninic Nov 02 '12 at 15:20