var tree = {
decorate:function(){
console.log('make some decorates on the tree!');
}
};
tree.getDecorate('RedBall');
tree.getDecorate('Angle');
tree.getDecorate('BlueBall');
tree.decorate();
我们需要给这个tree方法加一些装饰器,装饰器该怎么写呢?
tree.getDecorate = function(deco){
tree[deco].prototype = this;
returnnew tree[deco];
};
tree.RedBall = function(){this.decorate = function(){this.RedBall.prototype.decorate();
alert('add a redball on the tree!');
}
};
tree.BlueBall = function(){this.decorate = function(){this.BlueBall.prototype.decorate();
alert('add a blueball on the tree!');
}
};
tree.Angle = function(){this.decorate = function(){this.Angle.prototype.decorate();
alert('there is an anggle on the tree now!');
}
};