just some beginner level javascript I thought was cool
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.开发者_StackOverflowlog( 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?
This is a bad pattern. Basically you are giving methods a non-human readable name. You might as well write your functions like this:
Bad practise example:
function x1(A,B){
// DO SOMETHING
}
function x2(A){
// DO SOMETHING ELSE
}
Sticking it to a human readable namespace doesn't help matters.
However, it can be the first code part of a Command pattern ;) See objection.command
精彩评论