The best and Proper Way to code in Javascript is not to use Globals

Hi Kids,

You must avoid using globals in Javacript, its because
They are slower to look up than local variables and will clutter up the global namespace as well.

So here take a look kids. This is how you do it properly. 🙂

(function(){      
      var createObject = function(){
            var count = 0;
            var todo1 = function(){
                count += 1;
            };

            var todo2 = function(){
                count += 2;
            };

            return {
                task1: todo1,
                task2: todo2
            }
      };

      var obj = createObject();
      obj.task1();
      obj.task2();
}());

Alright Kids! Ciao!

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *