Hi

can someone please explain me whats the need of prototyping in javascript?

What I mean to say is, when you add a method/property to an object using "Function.prototype" syntax, why cannot you directly add that method inside that object when creating it?

Consider the following example:

Instead of doing this:
Code:
myobj = function(fname){
 this.fname = fname;
};

myobj.prototype.age = function (age) {
   this.age = age;
};

var x = new myobj('joe');
alert(x.fname);
x.age = 32;
alert (x.age);
I can do this:
Code:
myobj = function(fname, age){
 this.fname = fname;
  this.age = age;
};



var x = new myobj('joe', 32);
alert(x.fname);
alert (x.age);
So what do we need prototype for?

Any explanations will be appreciated.

Thanks