hi,
thanks very much, my appology for such a late reply,
Now my doubts are,
as in the above post mentioned,
1> Person.prototype.init.call(this, firstName, lastName, age),
here "this" will be from were the above line is getting called ie.
Employee instance,
2> now when i called Person.prototype.init(firstName, lastName, age),
the "this" parameter points to the person instance,
now how the second case will effect the inheritance hierarchy chain,
can you please put some light on this
On Nov 4, 1:54 pm, Cédric Bertolini <[email protected]>
wrote:
> Hi,
>
> It's my first post here. Please forgive me if I'm doing something
> wrong. English is not my first language.
>
> To answer your question, Rahul, about the difference between
> Person.prototype.init.call(this, firstName, lastName, age) and
> Person.prototype.init(firstName, lastName, age):
>
> In both expressions the same function is executed:
> Person.prototype.init. The difference is about what is "this" during
> the execution of the function. In the first case (with Function#call),
> you're telling the function to execute with "this" being "this" of the
> context of calling (first argument to Function#call). In the second
> case, the function is executed with "this" being Person.prototype
> (execution of member function).
>
> 2011/11/4 Rahul <[email protected]>
>
>
>
>
>
>
>
>
>
> > sorry, i mentioned wrong,
>
> > this is the correction as follows
>
> > Than i did Person.prototype.init.call(this, firstName, lastName, age)
> > in my init() of Employee.prototype,
>
> > On Nov 4, 12:15 pm, Rahul <[email protected]> wrote:
> > > 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 <[email protected]> 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 <[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]
--
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]