javascript - javascript 中的重写方法

标签 javascript overriding prototype

我的问题是我不明白为什么这个覆盖在这里不起作用

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());
   }

最佳答案

如果我理解你的问题,你注释掉的代码不起作用,因为它是在覆盖 GetFullName 之前执行的。

/* 
   **** 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());
   }

关于javascript - javascript 中的重写方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135029/

相关文章:

javascript - 逻辑和运算符在 JavaScript 中无法正常工作

javascript - 如何在 Webpack 项目中查找未使用的文件?

php - 乔姆拉!模型覆盖

Java:为什么不覆盖 "this( )"?

Javascript 扩展函数返回的内容?

javascript - 为什么使用 Javascript 对象而不是原型(prototype)

javascript - 如何正确设计 JS 子伪类的原型(prototype)?

php - 将新行从 PHP 替换为 JavaScript

javascript - 如何在 switch 语句和另一个函数中使用 const?

javascript - 向现有 Javascript 对象添加方法的最佳方式