-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_实现方法重载.html
More file actions
93 lines (77 loc) · 2.28 KB
/
Copy path06_实现方法重载.html
File metadata and controls
93 lines (77 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>实现方法重载</title>
</head>
<body>
</body>
<script>
// 网页输出日志
console.slog = console.log;
console.log = function(text,...args){
let others = [];
args.forEach(function(item){
if (typeof item == 'string'){
others.push(`"${item}"`);
}else{
others.push(item);
}
})
let body = document.getElementsByTagName('body')[0];
let node = document.createElement('pre');
node.textContent = `${text} ${others.join(" ")}`;
body.appendChild(node);
console.slog(text,...args);
}
</script>
<script>
// JS本身是没有方法重载的概念,如果当一个方法有很多种重载的时候,再手动去判断 arguments.length 去 switch 的话,那就是很痛苦的
// 添加方法实现重载
// 第一次调用时, old = undefined
// 第二次调用时, old = find
// 第三次调用时, old = find1
// 每次调用都是缓存上一次的 find 方法,所以当find1 找不到时,会去调用 find方法
function addMethod(obj,name,fn){
// 精髓之处
var old = obj[name];
obj[name] = function(){
// 判断调用的方法参数长度是否和 addMethod调用时传进来的fn的参数一至
if (fn.length == arguments.length){
// 调用该方法,apply 和 call功能一样,一个是数组传参,一个是单独参数一个一个传
return fn.apply(this,arguments);
}else{
// 调用上一个方法,一条链式调用,一直往上
return old.apply(this,arguments);
}
}
}
// 要添加方法的对象
var person = {
like: ['学习','听书','打球']
}
// 方法一
var find0 = function(){
console.log(this.like,'find0');
}
// 方法二
var find1 = function(name){
var index = this.like.indexOf(name);
console.log(index,'find1索引');
}
// 方法三
var find2 = function(name,age){
console.log('你要找的:'+ name +'和 '+age ,'find2');
}
// 添加方法
addMethod(person,'find',find0);
addMethod(person,'find',find1);
addMethod(person,'find',find2);
// 调用方法
person.find();
person.find("听书");
person.find("听书",18);
</script>
</html>