Factory function

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

var johnDoe = {
	firstName : ”John”,
	lastName : ”Doe”,
	sayHi  : function(){
		return “Hi there”;
	}
};

var janeDoe = {
	firstName : ”Jane”,
	lastName : ”Doe”,
	sayHi  : function(){
		return “Hi there”;
	}
}

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.

var createPerson = function(fName , lName){
	return {
		firstName : fName,
		lastName : lName,
		sayHi : function(){
			return “Hi there”;
		}
	};
};

var johnDoe = createPerson(“John”,”Doe”);

var janeDoe = createPerson(“Jane”,”Doe”);

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.