js模拟多态

我们知道js是弱类型语言,但是js可以模拟出来面向对象的(继承,封装,多态)

js实现多态


var makeSound = function(animal) {
    animal.sound();
}
 
var Duck = function(){}
Duck.prototype.sound = function() {
    console.log('嘎嘎嘎')
}
var Chiken = function() {};
Chiken.prototype.sound = function() {
    console.log('咯咯咯')
}
 
makeSound(new Chicken());

 

你可能感兴趣的:(js模拟多态)