javascript设计模式 - 多态
2016-07-31 00:00
351 查看
摘要: 多态背后的思想是将“做什么" 和”谁去做以及怎么做“ 分离开来。也就是将”不变的事物“ 和 ”可能改变的事物“ 分离开来。
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()); //咯咯咯
相关文章推荐
- JavaScript设计模式与开发实践——JavaScript的多态
- JavaScript设计模式Item 1—多态
- JavaScript设计模式Item 1—多态
- JavaScript设计模式基础-----封装、继承、多态
- javascript设计模式与开发实践(一)- 多态
- 《javascript设计模式与开放实践》学习(一)javascript实现多态2
- JavaScript设计模式与开发实践——JavaScript的多态
- 《javascript设计模式与开放实践》学习(一)javascript实现多态1
- 多态
- javascript设计模式-掺元类
- javascript设计模式和构造函数返回值
- java 多态
- C++中虚函数和多态
- java中的多态
- Java多态的一些陷阱
- Java多态中的若干问题
- 面向对象第三个特征----多态III(特点)
- .NET项目开发—浅谈面向对象的纵横向关系、多态入口,单元测试(项目小结)
- 继承和多态-----虚函数(一)
- Lesson4上机练习-----多态