I was thinking about design patterns lately, and I had an idea about one I hadnt came across yet (im sure it exists), so I tried to make it, and to my surprise it was easier than I thought. I thought it was cool, so Id like to share it with ya.
//pass this object a function, and it adds its
//biological and technological distinctiveness to itself
var snowman = {
snowballs: [],
addsnow: function(snow) {
this.snowballs.push(snow);
},
getsnow: function(index) {
con(this.snowballs[index]);
}
}
function squareArea(x, y) {
return x * y;
}
function circleArea(r) {
return Math.PI * 2 * r;
}
snowman.addsnow(squareArea);
snowman.addsnow(circleArea);
console.log( snowman.snowballs[0](5,3) );//15
console.log( snowman.snowballs[1](3) );//18 or so
snowman.getsnow(0);
What practical uses do you think it could have? What do you think about the idea of objects cannibalizing other objects?