Ik heb de volgende testcase, waarvan beide dispatches alert('b') teruggeven, waarvan ik wil dat de eerste netjes alert('a') terug gaat geven. Hoe kan ik zorgen dat de xmlString by value wordt doorgegeven en niet by reference
code:
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
| <script type="text/javascript">
function EventManager(){
this.types = new Array();
}
EventManager.prototype.addListener = function(type,action,lifespan){
if(this.types[type] == null){
this.types[type] = new EventQueue();
}
this.types[type].add(action,lifespan);
}
EventManager.prototype.dispatch = function(type){
if(this.types[type] != null){
var eventQueue = this.types[type];
var events = eventQueue.getEvents();
for(var i=0,j=eventQueue.getLength();i<j;i++){
events[i].action();
switch(events[i].lifespan){
// once : event triggered only once
// default : event is always triggered
case 'once':
events[i] = null;
break;
}
}
}
}
EventManager.prototype.getListeners = function(type){
if(this.types[type] != null){
return this.types[type];
}else{
return null;
}
}
function EventQueue(){
this.events = new Array();
}
EventQueue.prototype.add = function(action,lifespan){
this.events.push({action:action,lifespan:lifespan});
}
EventQueue.prototype.getEvents = function(){
return this.events;
}
EventQueue.prototype.getLength = function(){
return this.getEvents().length;
}
function foo(){
var self = this;
this.eventManager = new EventManager();
var xmlString = 'alert(\'a\')';
var func = function(){new Function(xmlString).call(self)};
this.eventManager.addListener('a',func);
xmlString = 'alert(\'b\')';
func = function(){new Function(xmlString).call(self)};
this.eventManager.addListener('b',func);
}
foo.prototype.dispatch = function(){
this.eventManager.dispatch('a');
this.eventManager.dispatch('b');
}
var test = new foo();
test.dispatch();
</script> |