Tag Archives: Javascript


I like the concept of closure, even though most of us think that it is complex topic. I would not go to the detail side of the closure but I would like to let you know by using an example. I was using closure unknowingly from past 4 years. Lets cut the chase and come to the point.


Dont you observe, I have called the function foo() once, but it is maintaining the value of a.

So, simple definition says that, when you define any function inside a function, you are using closure, And closure function keeps the copy of all the local variable passed to them.

Thats It. Ya, Thats It.


Factory Function in Javascript

What is factory function? And before this question, whatever it is, why I am going to use it. Hah!

Let us consider this example

I am creating two persons objects like

We created two objects so what if I want to create 10 more person object. Should I write these lines 10 more time? Opps! Sorry!

This is the situation where we are going to use factory function. In Javascript there are two function by which we can create object, one is constructor function and another is factory function.

If we want to create multiple objects with same interface, this is the better way. Remember, Interface is a signature. Hope you understood what I am trying to say.


Creating Objects in Javascript

As Javascript is object oriented language, there must be a concept of object. Then how do we create object in javascript.

Have a look on the old way…

var person = new Object();



person.sayHi = function(){

return “Hi there”;


Let us test this code

>> person.firstName

<< “John”

>> person.lastName

<< “Doe”

>> person.sayHi();

>> “Hi there”

But this doesn’t look so good, even though its easy. So have a look on new style called Object literal notation.

var person = {};


var person = {

firstName : “John”,

lastName : “Doe”,

sayHi : function(){

return “Hi there”;



Are you ready to test this, I think you are

>> person.sayHi();

<< “Hi there”

So why we use object literal notation

  • Performance : Javascript executes a single complex statement much faster than many simple statements.
  • Organization : Everything about an object well be in single bunch of code.
  • Number of keystrokes we type is much less.

Please write your comments and I am very well come for your queries.