-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo7.js
105 lines (85 loc) · 2.42 KB
/
demo7.js
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
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Created by Administrator on 2017/8/27 0027.
*/
//原型模式 用原型实例指向创建对象的类,使用于创建新的对象的共享原型对象的属性及方法
var LoopImages = function(imgArr,container){
this.imagesArray = imgArr;
this.container = container;
this.createImage = function(){
}
this.changeImage = function(){
}
}
var SlideLoopImg = function(imgArr,container){
LoopImages.call(this,imgArr,container);
this.changeImage = function(){
console.log("SlideLoopImg changeImage function")
}
}
var FadeLoopImg = function(imgArr,container,arrow){
LoopImages.call(this,imgArr,container);
this.arrow = arrow;
this.changeImage = function(){
console.log("FadeLoopImg changeImage function")
}
}
var fadeImg = new FadeLoopImg(['1.jpg','2.jpg','3.jpg'],'slide',['left.jpg','right.jpg'])
fadeImg.changeImage()
//优化一
var LoopImages = function(imgArr,container){
this.imagesArray = imgArr;
this.container = container;
}
LoopImages.prototype = {
createImage : function(){
console.log("LoopingImg is created")
},
changeImage : function(){
console.log("LoopingImg is changed")
}
}
var SlideLoopImg = function(imgArr,container){
LoopImages.call(this,imgArr,container);
}
SlideLoopImg.prototype = new LoopImages();
SlideLoopImg.prototype.changeImage = function(){
console.log("SlideLoopImg changeImage function")
}
var FadeLoopImg = function(imgArr,container,arrow){
LoopImages.call(this,imgArr,container);
this.arrow = arrow;
}
FadeLoopImg.prototype = new LoopImages();
FadeLoopImg.prototype.changeImage = function(){
console.log("FadeLoopImg changeImage function")
}
var fadeImg = new FadeLoopImg(['1.jpg','2.jpg','3.jpg'],'slide',['left.jpg','right.jpg'])
fadeImg.changeImage()
//复制多个对象模板的属性
function prototypeExtend(){
var F = function(){},args = arguments, i= 0,len = args.length;
for(;i < len ; i++){
for(var j in args[i]){
F.prototype[j] = args[i][j]
}
}
return new F()
}
var penguin = prototypeExtend(
{ speed : 20,
swim : function(){
console.log("游泳速度为: "+this.speed)
}
},
{ run : function(speed){
console.log("奔跑速度为: "+speed)
}
},
{
jump: function () {
console.log("跳跃动作")
}
})
penguin.swim();
penguin.run(10);
penguin.jump()