简体   繁体   English

JavaScript中的多态性示例用法错误

[英]Polymorphism Example usage error in JavaScript

This is an example to demonstrate polymorphism example, at which if I remove following lines: 这是一个演示多态性示例的示例,如果我删除了以下几行:

Employee.prototype= new Person();
Employee.prototype.constructor=Employee;

there is still no effect on the program and it gets the similar results. 对该程序仍然没有影响,并且得到了相似的结果。 If so, how does this example demonstrates polymorphism? 如果是这样,此示例如何证明多态性? Commenting those lines, I see there are 2 functions which when call return results based on their own getInfo functions; 注释一下这些行,我看到有两个函数,它们在调用时基于它们自己的getInfo函数返回结果。 so, where is the magic? 那么,魔术在哪里?

HTML: HTML:

<script type="text/javascript">
  function Person(age, weight) {
    this.age=age;
    this.weight=weight;
    this.getInfo=function() {
      return "I am " + this.age + " years old " +
        "and weighs " + this.weight +" kilo.";
    }
  }
  function Employee(age, weight, salary){
    this.salary=salary;
    this.age=age;
    this.weight=weight;
    this.getInfo=function() {
      return "I am " + this.age + " years old " +
        "and weighs " + this.weight +" kilo " +
        "and earns " + this.salary + " dollar.";
    }
  }
  Employee.prototype= new Person();
  Employee.prototype.constructor=Employee;
// The argument, 'obj', can be of any kind
// which method, getInfo(), to be executed depend on the object
// that 'obj' refer to.
  function showInfo(obj) {
    document.write(obj.getInfo()+"<br>");
  }
  var person = new Person(50,90);
  var employee = new Employee(43,80,50000);
  showInfo(person);
  showInfo(employee);
</script>

Result 结果

在此处输入图片说明

Reference 参考

http://w3processing.com/index.php?subMenuItemId=329 http://w3processing.com/index.php?subMenuItemId=329

This is really a weak example of polymorphism, besides the issue you have noticed (ie Employee.prototype setup is superflous, the prototype functions are never used in the example), here are a couple of other issues I see: 这确实是一个多态性的弱示例,除了您已经注意到的问题(即Employee.prototype设置非常繁琐,示例中从未使用原型函数)之外,还有以下两个其他问题:

  1. The Employee function (ie the constructor) never calls the 'base class' constructor ie Person function - you would expect this to be in place given that they are in a parent-child relationship. Employee函数(即构造函数)从不调用“基类”构造函数(即Person函数)-如果它们处于父子关系,则您希望此函数就位。
  2. As a consequence of the above issue, the Employee function has code copy-pasted from the Parent function: 由于上述问题, Employee函数具有从Parent函数复制粘贴的代码:

    this.salary=salary; this.age=age; this.weight=weight;

  3. Idem for the getInfo function - both constructors create their specific functions and assign it to this ; 同上针对getInfo功能-两个构造创建它们的具体功能,并将其分配给this ; and the Employee version of getInfo never invokes the base class version - which it should maybe, to demonstrate inheritance 并且getInfoEmployee版本从不调用基类版本-可能应该演示基类

    So, really the only thing can be said in favor of this example is that it uses the same function name ie getInfo on the Person and Employee and invokes them on the two objects using common code. 因此,实际上唯一可以支持此示例的事情是,它使用相同的函数名称,即PersonEmployee上的getInfo ,并使用通用代码在两个对象上调用它们。 The can be termed as an example of polymorphism but probably an elementary one 可以将其称为多态的示例,但可能是基本的

Inheriting "methods" 继承“方法”

JavaScript does not have "methods" in the form that class-based languages define them. JavaScript没有以基于类的语言定义它们的形式的“方法”。 In JavaScript, any function can be added to an object in the form of a property. 在JavaScript中,可以将任何函数以属性的形式添加到对象。 An inherited function acts just as any other property, including property shadowing as shown above (in this case, a form of method overriding). 继承的函数的作用与其他任何属性一样,包括上面所示的属性阴影(在这种情况下,是一种方法重写)。

When an inherited function is executed, the value of this points to the inheriting object, not to the prototype object where the function is an own property. 当执行继承的函数时,此函数的值指向继承的对象,而不指向函数是其自身属性的原型对象。

var o = {
      a: 2,
      m: function(b){
        return this.a + 1;
      }
    };

    console.log(o.m()); // 3
    // When calling o.m in this case, 'this' refers to o

    var p = Object.create(o);
    // p is an object that inherits from o

    p.a = 4; // creates an own property 'a' on p
    console.log(p.m()); // 5
    // when p.m is called, 'this' refers to p.
    // So when p inherits the function m of o, 
    // 'this.a' means p.a, the own property 'a' of p

you can find what you looking for in MDN LINK 您可以在MDN LINK中找到所需的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM