侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

javascript设计模式 - 多态

2024-05-07 星期二 / 0 评论 / 0 点赞 / 57 阅读 / 765 字

var makeSound = function(animal){if( animal instanceof Duck){console.log('嘎嘎嘎');}else if(animal inst

var makeSound = function(animal){if( animal instanceof Duck){console.log('嘎嘎嘎');}else if(animal instanceof Chicken){console.log('咯咯咯');}}; var Duck = function(){};var Chicken = function(){};makeSound(new Duck());  //嘎嘎嘎makeSound( new Chicken()); //咯咯咯
var makeSound = function(animal){animal.sound();}//然后把可变的部分各自封装起来var Duck = function(){}Duck.prototype.sound = function(){console.log('嘎嘎嘎');};var Chicken = function(){}Chicken.prototype.sound = function(){console.log('咯咯咯');};makeSound(new Duck());	//嘎嘎嘎makeSound(new  Chicken());		//咯咯咯

 

广告 广告

评论区