-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomposite.js
44 lines (36 loc) · 1.12 KB
/
composite.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
var noSuchMethod = function(methodName, args){
this[methodName] = function(){ // create iterative function and cache it
for(var i = 0, l = this.items.length; i != l; i++){
var item = this.items[i];
if(typeof item[methodName] == 'function'){
item[methodName].apply(item, arguments);
}
}
};
this[methodName].apply(this, args);
};
var Composite = function(){
return {
__noSuchMethod__ : noSuchMethod,
items : Array.prototype.slice.apply(arguments)
};
}
/* tests
var circle = {
draw : function(color){ console.log(color + ' circle'); }
};
var square = {
draw : function(color){ console.log(color + ' square'); }
};
var triangle = {
draw : function(color){ console.log(color + ' triangle'); }
};
var groupOfShapes = Composite(circle, square, triangle);
groupOfShapes.draw('red');
var ellipse = {
draw : function(color){ console.log(color + ' ellipse'); }
};
var not_drawable = {}; // will be ignored silently
var biggerGroup = Composite(ellipse, not_drawable, groupOfShapes) // nested composite
biggerGroup.draw('green');
//*/