i did what u said, i swapped the calls, the call of init() went into
recursive mode,

Than i did Person.prototype.init.call(this, firstName, lastName, age)
in my init() of Manager.prototype,

it worked fine.

But than one question, why i call init() of Person in the above way,
rather than straight call of function like this

Person.prototype.init(firstName, lastName, age);

this also worked fine an gave me the expected result.

On Nov 3, 7:59 pm, Jake Verbaten <rayn...@gmail.com> wrote:
>                        Employee.prototype.init =
> function(firstName,lastName,age,employeeId){
>                                alert("INIT-METHOD OF EMPLOYEE-PROTOTYPE ");
>                                if(arguments.length > 0){
>                                        this.init(firstName,lastName,age);
>                                        this.empId = employeeId;
>                                }
>                        }
>
>                        Employee.prototype = new Person();
>
> Here you are overwriting `Employee.prototype` after writing the init
> method. You will need to swap these two segments.
>
> Even then it won't work as you expect because `this.init ===
> Employee.prototype.init` and thus infinite recursion
>
> You need a `Person.prototype.init.call(this, firstName, lastName, age)`
>
>
>
>
>
>
>
> On Thu, Nov 3, 2011 at 2:49 PM, Rahul <rahulshivsha...@gmail.com> wrote:
> > hi friends ,
> > please follow the code below,
>
> >                        function Person(firstName,lastName,age){
> >                                alert(" Person constructor called ");
> >                                if(arguments.length > 0){
> >                                        this.init(firstName,lastName,age);
> >                                }
> >                        }
>
> >                        Person.prototype.toString = function(){
> >                                return  "-> FIRST-NAME "+this.firstName+
> >                                                "\n LAST-NAME
> > "+this.lastName;
> >                        }
>
> >                        Person.prototype.init = function(fName,lName,age){
> >                                alert("INIT-METHOD OF PERSON-PROTOTYPE ");
> >                                this.firstName = fName;
> >                                this.lastName = lName;
> >                                this.age = age;
> >                        }
>
> >                        function
> > Employee(firstName,lastName,age,employeeId){
> >                                alert(" Employee constructor called ");
> >                                if(arguments.length > 0){
> > this.init(firstName,lastName,age,employeeId);
> >                                }
> >                        }
>
> >                        Employee.prototype.init =
> > function(firstName,lastName,age,employeeId){
> >                                alert("INIT-METHOD OF EMPLOYEE-PROTOTYPE ");
> >                                if(arguments.length > 0){
> >                                        this.init(firstName,lastName,age);
> >                                        this.empId = employeeId;
> >                                }
> >                        }
>
> >                        Employee.prototype = new Person();
>
> >                        Employee.prototype.toString = function(){
> >                                return  "<*> FIRST-NAME "+this.firstName+
> >                                                "\n LAST-NAME
> > "+this.lastName+
> >                                                "\n AGE "+this.age+
> >                                                "\n EMP-ID "+this.empId;
> >                        }
>
> >                        Manager.prototype = new Employee();
>
> >                        function
> > Manager(firstName,lastName,age,id,deptName){
> >                                alert(" Manager constructor called ");
>
> >                                if(arguments.length > 0){
>
> >  this.init(firstName,lastName,age,id);
> >                                        this.deptName = deptName;
> >                                }
> >                        }
>
> >                        Manager.prototype.toString = function(){
> >                                return  "(*) FIRST-NAME "+this.firstName+
> >                                                "\n(*) LAST-NAME
> > "+this.lastName+
> >                                                "\n(*) AGE "+this.age+
> >                                                "\n(*) EMP-ID "+this.empId
> >                                                "\n(*) DAPARTMENT-NAME
> > "+this.deptName;
> >                        }
>
> >                        function fn37(){
> >                                var manager = new
> > Manager("Hemesh","Kataria",
> > 45,"INST3434","Instrumentation");
> >                                alert(manager);
> >                        }
>
> >                        fn37();
>
> > now what i am expecting is that my Manager should call internally
> > init() of Employee and than the init() method of Employee calls init()
> > method of Person,
>
> > But whats happening is init() of Person is getting called,
>
> > and the alert show the "id" property as "undefined",
>
> > please tell me were i am going wrong
>
> > --
> > To view archived discussions from the original JSMentors Mailman list:
> > http://www.mail-archive.com/jsment...@jsmentors.com/
>
> > To search via a non-Google archive, visit here:
> >http://www.mail-archive.com/jsmentors@googlegroups.com/
>
> > To unsubscribe from this group, send email to
> > jsmentors+unsubscr...@googlegroups.com

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to