0

My problem here that i don't understand why this override do not work here is the source

window.onload=function()
{
   function Person(first,last)
   {
      this.First=first;
      this.Last = last;
      this.Full=function()
      {
         return this.First+" "+this.Last;
      };
   }

   Person.prototype.GetFullName=function()
   {
      return this.First + " " + this.Last;
   } ;



   function Employee(first,last,position)
   {
      Person.call(this,first,last);
      this.Position=position;
   }
   /* Employee.prototype = new Person();
   var t = new Employee("Mohamed","Zaki","Web Developer");

   alert(t.GetFullName());
    */
   Employee.prototype.GetFullName=function()
   {
      var x = Person.prototype.GetFullName.call(this);
      return x + " , " + this.Position ; 
   }
   var e = new Employee("Mohamed","Zaki","Web Developer");
   alert(e.GetFullName());
   }
6
  • When I run this code (after adding a final }), it alerts Mohamed Zaki , Web Developer. What were you expecting? Commented Apr 13, 2012 at 4:02
  • i know the code is working but i ask about the code in comment,, why it doesn't work ? Commented Apr 13, 2012 at 4:10
  • because even though you set this.Position=position, nothing ever reads it. were you expecting it to be in GetFullName? maybe you intended to include more code in the comment? Commented Apr 13, 2012 at 4:16
  • i didn't ask about Position ...i just ask why the code in the comment doesn't work ? Commented Apr 13, 2012 at 4:25
  • if we uncomment this code it won't work (the message doesn't appear) Commented Apr 13, 2012 at 4:33

2 Answers 2

1

If I understand your question, the code that you've commented out doesn't work because it is executed before GetFullName is overridden.

/* 
   **** this code is executed before GetFullName is overridden and will use
   **** original function 
   Employee.prototype = new Person();
   var t = new Employee("Mohamed","Zaki","Web Developer");

   alert(t.GetFullName());
    */

   Employee.prototype.GetFullName=function()
   {
      var x = Person.prototype.GetFullName.call(this);
      return x + " , " + this.Position ; 
   }

   /**** This code is executed after GetFullName is overridden uses the new version */
   var e = new Employee("Mohamed","Zaki","Web Developer");
   alert(e.GetFullName());
   }
Sign up to request clarification or add additional context in comments.

Comments

1

First, remove the window.onload wrapper because it's not doing anything useful. Explanation below:

  function Person(first,last) { 
    this.First = first; 
    this.Last = last; 
    this.Full = function() { 
      return this.First + " " + this.Last; 
    }; 
  } 

  Person.prototype.GetFullName = function() {  
    return this.First + " " + this.Last; 
  } ; 

  function Employee(first,last,position) { 
    Person.call(this,first,last); 
    this.Position = position; 
  } 
  Employee.prototype = new Person(); 

  var t = new Employee("Mohamed","Zaki","Web Developer"); 

  // Here getFullName is called before the new method is added
  // to Person.prototype so you only get first and last name
  alert(t.GetFullName()); 

  // Here is where the new method is added
  Employee.prototype.GetFullName=function() { 
    var x = Person.prototype.GetFullName.call(this); 
    return x + " , " + this.Position ; 
  }

  var e = new Employee("Mohamed","Zaki","Web Developer"); 

  // so here you get first, last and position
  alert(e.GetFullName()); 

  // And existing instances get the new method too
  alert(t.GetFullName()); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.