如何在JavaScript中使用多态性



大多数语言都使用单个继承。而且很明显执行此操作的模式(例如,在下面的Swift代码中)。我仍在尝试将头缠绕在JavaScript中的模式中,以创建对象层次结构并重复使用类功能,并覆盖类函数

class animal {
    func talk() {
        print ("?")
    }
}
class bird : animal {
    override func talk() {
        print("tweet tweet")
    }
    func fly() {
        print("flap flap")
    }
}
class parrot : bird {
    override func talk() {
        print("polly want a cracker")
    }
}
var a = animal()
var b = bird()
var p = parrot()
a.talk()  /// ?
b.talk()  /// tweet tweet
b.fly()   /// flap flap
p.talk()  /// polly want a cracker
p.fly()   /// flap flap

我认为我的问题是JavaScript代码看起来并非如此。等效的JavaScript代码是什么,因此我可以找出模式?

您几乎回答了自己的问题。您只需要学习JavaScript的语法即可。

我认为我的问题是JavaScript代码看起来并非如此。

  1. 如果一种语言看起来与另一种语言不同

  2. ,这不是一个"问题"
  3. 您提供的Swift代码在句法上非常接近JavaScript(ES6),您需要写入以表达类的类别

class Animal {
  talk() {
    console.log('?')
  }
}
class Bird extends Animal {
  talk() {
    console.log('tweet tweet')
  }
  fly() {
    console.log('flap flap')
  }
}
class Parrot extends Bird {
  talk() {
    console.log('polly want a cracker')
  }
}
var a = new Animal()
var b = new Bird()
var p = new Parrot()
a.talk()
b.talk()
b.fly()
p.talk()
p.fly()


如果要在 es5 中设置"类"继承,则可以执行此操作

// Animal "class"
function Animal() {}
// Animal methods
Animal.prototype.talk = function talk() {
  console.log('?')
}
// ------------------------------
// Bird "class"
function Bird() {
  // if you want to call the parent constructor, you can do that here
  // Animal.call(this, arg1, arg2, ... argN)
}
// Bird inherits from Animal
Bird.prototype = Object.create(Animal.prototype)
Bird.prototype.constructor = Bird
// Bird methods
Bird.prototype.talk = function() {
  console.log('tweet tweet')
}
Bird.prototype.fly = function() {
  console.log('flap flap')
}
// ------------------------------
// Parrot "class"
function Parrot() {
  // if you want to call the parent constructor, you can do that here
  // Bird.call(this, arg1, arg2, ... argN)
}
// Parrot inherits from Bird
Parrot.prototype = Object.create(Bird.prototype)
Parrot.prototype.constructor = Parrot
// Parrot methods
Parrot.prototype.talk = function() {
  console.log('polly want a cracker')
}
var a = new Animal()
var b = new Bird()
var p = new Parrot()
a.talk()
b.talk()
b.fly()
p.talk()
p.fly()

有两个答案,es6:

class animal {
    talk() {
        console.log("?")
    }
}
class bird extends animal {
    talk() {
        console.log("tweet tweet")
    }
    fly() {
        console.log("flap flap")
    }
}
class parrot extends bird {
    talk() {
        console.log("polly want a cracker")
    }
}
var a = new animal()
var b = new bird()
var p = new parrot()
a.talk()  /// ?
b.talk()  /// tweet tweet
b.fly()   /// flap flap
p.talk()  /// polly want a cracker
p.fly()   /// flap flap

和ES5:

function animal() {
}
animal.prototype.talk = function () {
    console.log("?")
};
function bird() {
    animal.call(this)
}
bird.prototype = Object.create(
    animal.prototype,
    {constructor: {value: bird}}
);
bird.prototype.talk = function () {
    console.log("tweet tweet")
};
bird.prototype.fly = function () {
    console.log("flap flap")
};
function parrot() {
    bird.call(this);
}
parrot.prototype = Object.create(
    bird.prototype,
    {constructor: {value: parrot}}
);
parrot.prototype.talk = function () {
    console.log("polly want a cracker")
};
var a = new animal()
var b = new bird()
var p = new parrot()
a.talk()  /// ?
b.talk()  /// tweet tweet
b.fly()   /// flap flap
p.talk()  /// polly want a cracker
p.fly()   /// flap flap

最新更新