-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdecorator.html
53 lines (53 loc) · 1.2 KB
/
decorator.html
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
<html>
<head>
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.js"></script>
<style type="text/css">
.demo{
border:1px solid coral;
padding:30px;
margin:5px;
}
</style>
</head>
<body>
<div>decorator设计模式</div>
<div class="demo decorator">
decorator举例子
<button id="d-btn">
点击从
<span>0</span>
开始进行加法计算
</button>
<div id="decorator">
result is ---- <span>0</span>
</div>
<script type="text/javascript">
//日志系统decorator
var decorator_log = function(fn) {
return function(){
var result = fn.apply(this, arguments);
console.log('日志系统,加法计算参数是-- ');
console.log(arguments[0]);
return result
}
}
</script>
<script type="text/javascript">
//主功能
var Add = function(array) {
return array.reduce(function(pre,cur) {
return pre+cur
},0)
};
Add = decorator_log(Add)
var data=[1];
$('#d-btn').click(function(){
var nextData= data.length
data.push(nextData);
$('#decorator span').html(Add(data,nextData));
$('#d-btn span').html(nextData+1);
})
</script>
</div>
</body>
</html>