Click here to Skip to main content
15,936,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
please clarify the difference b/w this two codes
function Person(gender) {
        this.gender = gender;
        alert('Person instantiated');

    Person.prototype.sayHello = function()
        alert ('hello');

    var person1 = new Person('Male');
    var person2 = new Person('Female');
 // call the Person sayHello method.

and the second one is below where function define inside funciton (without prototype property)
function Animal(gender) {
       this.gender = gender;
       alert('animal instantiated');
       this.sayToodle =  function()
           alert ('GOOOOOOO!!');

   var Animal1 = new Animal('Male');
   var Animal2 = new Animal('Female');


my more simple question are:

i: what is the difference?
ii: define method inside or out side of a function. what is the effect?
iii: if both same then which is the fine way to define this.
iv: and what does prototype do?
Can we not define method of a obj out side of its function(CLASS)???
Updated 13-Jan-14 2:02am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900