Click here to Skip to main content
15,070,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
please clarify the difference b/w this two codes
JavaScript
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.
    person1.sayHello()

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



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


   Animal1.sayToodle();

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)???
Posted
Updated 13-Jan-14 2:02am
v2

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