Skip to content

Commit 53e297e

Browse files
update
1 parent ec01d78 commit 53e297e

File tree

4 files changed

+21
-29
lines changed

4 files changed

+21
-29
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
笔记:动态类型语言不必借助超类就能轻松实现一个原则:“面向接口编程,而不是面向实现编程”
22
<script type="text/javascript">
33
var duck = {
44
duckSinging: function(){
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<script type="text/javascript">
1+
笔记:反面教材
2+
<script type="text/javascript">
23

34
var makeSound = function( animal ){
45
if ( animal instanceof Duck ){
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<script type="text/javascript">
1+
笔记:改写后,把不变的部分隔离出来,makeSound
2+
<script type="text/javascript">
23

34
var makeSound = function( animal ){
45
animal.sound();
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,46 @@
1-

1+
笔记:多态的最佳实践 demo
22
<script type="text/javascript">
3-
43
var googleMap = {
54
show: function(){
65
console.log( '开始渲染谷歌地图' );
76
}
87
};
9-
10-
var renderMap = function(){
11-
googleMap.show();
12-
};
13-
14-
renderMap(); // 输出:开始渲染谷歌地图
15-
16-
var googleMap = {
8+
var baiduMap = {
179
show: function(){
18-
console.log( '开始渲染谷歌地图' );
10+
console.log( '开始渲染百度地图' );
1911
}
2012
};
21-
22-
var baiduMap = {
13+
var sosoMap = {
2314
show: function(){
24-
console.log( '开始渲染百度地图' );
15+
console.log( '开始渲染搜搜地图' );
2516
}
2617
};
2718

28-
var renderMap = function( type ){
19+
// 反面教材1
20+
var renderMap1 = function(){
21+
googleMap.show();
22+
};
23+
renderMap1(); // 输出:开始渲染谷歌地图
24+
25+
// 反面教材1
26+
var renderMap2 = function( type ){
2927
if ( type === 'google' ){
3028
googleMap.show();
3129
}else if ( type === 'baidu' ){
3230
baiduMap.show();
3331
}
3432
};
33+
renderMap2( 'google' ); // 输出:开始渲染谷歌地图
34+
renderMap2( 'baidu' ); // 输出:开始渲染百度地图
3535

36-
renderMap( 'google' ); // 输出:开始渲染谷歌地图
37-
renderMap( 'baidu' ); // 输出:开始渲染百度地图
38-
36+
// 推荐的方式
3937
var renderMap = function( map ){
4038
if ( map.show instanceof Function ){
4139
map.show();
4240
}
4341
};
44-
4542
renderMap( googleMap ); // 输出:开始渲染谷歌地图
4643
renderMap( baiduMap ); // 输出:开始渲染百度地图
47-
48-
var sosoMap = {
49-
show: function(){
50-
console.log( '开始渲染搜搜地图' );
51-
}
52-
};
53-
5444
renderMap( sosoMap ); // 输出:开始渲染搜搜地图
5545

5646
</script>

0 commit comments

Comments
 (0)