Repository navigation
Expand file tree
/
Copy pathJavaScript TPn°1.js
More file actions
119 lines (95 loc) · 2.81 KB
/
Copy pathJavaScript TPn°1.js
File metadata and controls
119 lines (95 loc) · 2.81 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function EventEmitter(){
this.callbacks = {};
}
EventEmitter.prototype = {
on: function(event, fn){
if(!this.callbacks.hasOwnProperty(event)){
this.callbacks[event] = [];
}
this.callbacks[event].push(fn);
return this;
},
off: function(event, fn){
if (!fn){
if (!event){
console.log('tout les events sont suprimés');
delete this.callbacks;
}
else {
console.log('toutes les fonctions de'+ event+ 'sont éfacés');
this.callbacks[event] = [];
}
}
else {
console.log('La fonction a été supprimé');
this.callbacks[event].splice(this.callbacks[event].indexOf(fn), 1)
}
return this;
},
emit: function(event){
// Récupérer tous les args sauf "event" dans un tableau
var args = Array.prototype.slice.call(arguments);
//shift enlève le premier élément d'un tableau
args.shift();
// Si "event" est présent dans this.callbacks
if(this.callbacks.hasOwnProperty(event)){
this.callbacks[event].forEach((function(f){
f.apply(this, args);
}));
}
return this;
},
once: function(event, fn){
var n = 0;
if(!this.callbacks.hasOwnProperty(event)){
this.callbacks[event] = [];
}
var fnthis = this;
var oncefn = function (){
++n;
if(n == 1){
fnthis.callbacks[event].splice((fnthis.callbacks[event].indexOf(oncefn) -1), 2);
}
}
this.callbacks[event].push(fn);
this.callbacks[event].push(oncefn);
return this;
},
times: function(event, num, fn){
if(!this.callbacks.hasOwnProperty(event)){
this.callbacks[event] = [];
}
var fnthis = this;
var timesfn = function (){
num--;
if(num === 0){
fnthis.callbacks[event].splice(fnthis.callbacks[event].indexOf(fn), 2);
}
}
this.callbacks[event].push(fn);
this.callbacks[event].push(timesfn);
return this;
}
};
//Création des variables
var julien = new EventEmitter();
var matthieu = new EventEmitter();
var malo = new EventEmitter();
var fn = console.log.bind(console);
julien.on("event1", console.log.bind(console))
julien.on("event2", console.log.bind(console))
julien.emit("event1", 1).emit("event2",2)
julien.off()
//julien.emit("event1", 1).emit("event2",2);*/
julien.on("event1", fn)
julien.on("event2", fn)
julien.emit("event1", 1).emit("event2", 2)
julien.off("event1", fn)
julien.emit("event1", 1).emit("event2", 2)
julien.once("event", function(){console.log("Should only be printed once");})
julien.emit("event");
julien.emit("event");
julien.times("event1", 2, fn)
julien.emit("event1", "hello should be print")
julien.emit("event1", "world should be print")
julien.emit("event1", "SHOULD NOT BE PRINTED");