0
我最近在考虑设计模式,并且我有一个我还没有遇到过的问题(我确定它存在)的一个想法,所以我试图做到这一点,并且让我吃惊这比我想象的要容易。我认为这很酷,所以我想与你分享。只是一些初学者的水平javascript我认为很酷
//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);
你认为它有什么实际用途?你如何看待其他物体相互作用的概念?
如果您是通过功能的阵列循环,你怎么会知道的功能多少参数需要? – 2010-08-14 04:16:49