相关疑难解决方法(0)

什么是Javascript中的多态?

我已经阅读了一些我可以在互联网上找到的关于多态性的文章.但我认为我无法理解它的含义及其重要性.大多数文章没有说明为什么它很重要以及如何在OOP中实现多态行为(当然在JavaScript中).

我无法提供任何代码示例,因为我还没有想到如何实现它,所以我的问题如下:

  1. 它是什么?
  2. 为什么我们需要它?
  3. 这个怎么运作?
  4. 如何在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 +" …
Run Code Online (Sandbox Code Playgroud)

javascript oop polymorphism functional-programming parametric-polymorphism

83
推荐指数
5
解决办法
8万
查看次数