-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decorator.html
54 lines (53 loc) · 1.51 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
54
<html><head><title>Decorator-装饰者模式</title><meta charset="utf-8"></head>
<body>
<script type="text/javascript">
var MyNamespace = window.MyNamespace || {};
// 定义在sample模块
MyNamespace.sample = (function(){
function buildDom(){}
buildDom.prototype = {
startBuilt: function(){
var body = document.getElementsByTagName('body')[0];
for(var i = 0; i < 100; i++){
var list = document.createElement('ul');
for(var j = 0; j < 100; j++){
var item = document.createElement('li');
var text = document.createTextNode('test');
item.appendChild(text);
list.appendChild(item);
}
body.appendChild(list);
}
}
}
function timeDetector(buildDom){
this.buildDom = buildDom;
this.startTime;
}
timeDetector.prototype = {
startRun: function(){
this.startTime = (new Date()).getTime();
},
stopRun: function(){
var runTime = (new Date()).getTime() - this.startTime;
console.log("running cost:" + runTime + 'ms');
},
startBuilt: function(){
this.startRun();
this.buildDom.startBuilt();
this.stopRun();
}
}
return {
buildDom: buildDom,
timeDetector: timeDetector
}
})()
window.onload = function(){
var buildDom = new MyNamespace.sample.buildDom();
var buildDom = new MyNamespace.sample.timeDetector(buildDom);
buildDom.startBuilt();
}
</script>
</body>
</html>