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 <[email protected]> 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/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]