Hiding Scope in Javascript

This is my first entry on Javascript. Over the years I used Basic, Turbo Pascal, Assembly, Perl, Python, C and C++, Java and C#. For most of that time I considered Javascript not really worthy my time. Somehow too loose for my taste.

It all changed once I embraced AJAX as integral part of the web application experience. Suddenly I realized that Javascript is actually quite useful, simple and powerful. In fact, now that I think of it, with a few concepts borrowed from Python or Matlab it could be the language for Machine Learning.

Anyways if you start learning Javascript they show you how to declare a variable and a function. From there you can slowly go deeper.

Variables in Javascript are similar to scalars in Perl or variants in Visual Basic. They can hold primitive data such as numbers, booleans or strings. Beyond primitive types a variable can represent an object or an array. If the properties of the variable are indexed by text it is an object if they are indexed by non-negative integers it is considered an array.

You realize that objects and arrays are kind of related when you try to enumerate the properties.

This foreach loop will enumerate both objects. For arrays key will be an index while for regular objects it will be property name.
So far so good. Javascript is simple. In its basic form Javascript does not have many of the advanced keywords that Java or C# have. For example there is no class keyword.
Instead you define a function that acts as a constructor and if you use the “new” operator. It effectively instantiates an object of that type.

Observe the special property “prototype”. If you assign functions to that property it adds them as class methods.

Observe something else. Something very interesting and important. Something that made me go AHA!.


And since functions are objects you can assign them as values to other object properties. Properties that hold functions as value can be invoked of course. That is why you can call method “test” at the bottom of the last example.

But that is not the end of the story. It is the beginning that made me realize how powerful Javascript can be.

You see functions are objects and any variables and functions that you define inside a function live inside the scope of that object.

What it means is easiest explained with an example. Let us use a skeleton of a jQuery plugin as that example.

In the above code we first define a no-name function or anonymous function and call it immediately providing jQuery variable as parameter.

(function ($) {})(jQuery);

Inside this anonymous function an object is created that holds various helper methods such as “destroy” and “init”. Also inside the method we assign a function to a new property of variable “$.fn” and name it “validator”.

As you can see we are registering a jQuery validation plugin.

Functions that are created inside other functions remember their parent function. This is unlike old fashioned languages like C++ where scope of a function or frame is pushed on the stack and removed as soon as the function returns.

In Javascript, as shown here, when we invoke the jQuery plugin some time later using “$(‘#elmentId’).validator(‘blabla’)” it will return a trivial function that returns false. But most importantly the returned function sees the variable “methods” and can access it.

Any other inner function also sees the variable. But to the outside world the variable is inaccessible. We have managed to hide internal state of a plugin from the outside world.

To put everything together here is a skeleton of a jQuery plugin that allows us to call internal method via parameter as is recommended.

In the method “$.fn.validator” we check if “methodOrOptions” is a property of “methods” variable. If it is we call the appropriate method and hand it all remaining arguments as there could be more than one.

The call to the selected method uses .each() iterator of jQuery except when method ‘verify’ is selected. Then it processes return values differently. For a validator this is useful because “verify” needs to return first false value after checking several elements. While all other methods are applied equaly to all elements selected by the jQuery selection using method call ” $(‘….’)”

The usage of a validation plugin built this way would then look like this:

Hopefully this tutorial helps you understand Javascript better. In the process we also mentioned jQuery and showed how jQuery plugins are structured.