JavaScript Diary I

Learn JavaScript fundamentals in this beginner-friendly guide. Master global variables, self-executing functions, and best practices for writing clean, maintainable code.

4 min read 683 words

Long back I wrote an article on CoffeeScript. If anyone sees closely, it's all about writing proper JavaScript in an easy way. But in many cases, it's not possible that you get a chance to write CoffeeScript. Or if you are learning some library like KnockoutJS or BackboneJS, and if you are using some framework like AngularJS, then using CoffeeScript and also using this function to learn, it will be difficult to catch up together if you are not powerful enough with one or the other.

And as a base rule: even if you are using CoffeeScript, you are not escaping from JavaScript. It was there and it will be there for sure. So, it is worth learning it. If you have read the previous article, you must know that I am not that good with JavaScript. So, I started learning and also thought that I should put that in a blog. Whatever I learn will be documented on my blog, so that it will be helpful to others and specially for me.

Most of the guys must be knowing many things. But that's fine, it's again for personal reference.

JavaScript is a very flexible language. And this allows you to write lovely code without worrying about many things. But again, this makes issues many times. And every damn time I will end up messing with that flexibility issue. flexible

And the first thing starts with global variables. In JavaScript, everything is global by default. So, whenever you are writing any function just like that, they are globally accessible. This is an issue, just like static variables in any other language. So, don't write functions just like that out anywhere on the page. A simple solution for this is a self-executing anonymous function. That will make everything private, and that's good at some point—at least better than putting everything global. Here, everything is private and executed. Just check out the example.

(function() { 
  var dosomething = function (){ 
  //Here I am doing something 
  } 
})(); 

Here, dosomething is completely private. But sometimes we need something in global. We need to access it from outside. Then there are two patterns for that: the Module Pattern and the Revealing Module Pattern. I personally prefer the Revealing Module Pattern. So, I am giving a demo for that.

var REVEALING; 
  REVEALING = ( function () { 
    var dosomething = function (){ 
      //Here I am doing something 
      } 
      var dosomethingmore = function (){ 
      // here I am accessing dosomething 
    dosomething(); 
    } 
  return 
    {   
    dosomethingmore:dosomethingmore 
    //here only one function is exposed 
    //so it is the only function available outside this namespace 
    } 
})(); 

Here, one more thing comes into the picture: namespace, which are not available by default in JavaScript.

As namespace is not there, we are missing classes too. As JavaScript is a functional language, it only has functions, also available as objects. So, if you like to make things like classes, internally they are functions only. And you can also create constructor-based functions.

var Dosomething = ( 
  function (){ 
    function Dosomething(parametername)   
    this.parametername = parametername; 
})(); 

So, now we have our lovely constructor with a parameter. While calling this, we can just do like this:

var dosomethingobject = new Dosomething( "namehere" );

And after that, we can call whatever functions we like to call. It will use the parameter. This also has the same benefit as we are getting with parameterized constructors in structured languages. So, these small things always help to write more readable code.

One other thing worth noting here: it is that we can write anything anywhere in JavaScript, but that does not mean that we should go for that. Even in JavaScript, the Single Responsibility Principle should be followed.

And also, parameter names and function names should be self-describing. Just because JavaScript is giving freedom, we shouldn't just use it like that.

There are many libraries and other corner cases that can be considered while developing a client-side JavaScript application. But that will be next time.

Till then, happy coding…

Frequently Asked Questions

What is a self-executing anonymous function in JavaScript?

A self-executing anonymous function is a function that runs immediately when it's defined, written as (function() { ... })(). It creates a private scope where variables and functions are not globally accessible, helping you avoid polluting the global namespace and reducing conflicts with other code.

Why should I avoid writing functions directly in the global scope?

Writing functions directly in the global scope makes them globally accessible, which can cause naming conflicts and unpredictable behavior similar to static variables in other languages. Using private scopes through self-executing functions or the Revealing Module Pattern keeps your code safer and more maintainable.

What is the Revealing Module Pattern in JavaScript?

The Revealing Module Pattern is a design pattern that allows you to keep some functions private while selectively exposing only specific functions to the global scope through a return statement. This gives you control over your public API while maintaining encapsulation for internal functions.

How do I create a namespace in JavaScript?

Since JavaScript doesn't have built-in namespaces, you can create them using the Module Pattern or Revealing Module Pattern by wrapping your code in an immediately-invoked function expression (IIFE) and returning an object with the properties you want to expose publicly.

Why is learning JavaScript important even if I use CoffeeScript?

CoffeeScript compiles to JavaScript, so you cannot escape JavaScript regardless of what language you write in. Understanding JavaScript fundamentals is essential for debugging, learning frameworks like AngularJS or BackboneJS, and becoming a more proficient developer.

Share this article