本文摘自 《JavaScript 设计模式》张容铭 著 版权归原作者所有
- 事件与业务逻辑之间的桥梁 类似中间件函数
function change(dom,color,bg){
dom.style.color = color;
dom.style.background = bg;
}
var spans = document.getElmentByTagName('span');
spans[0].onclick = fucntion(){
change(this,'#ddd','#0000');
}
- 多元化对象
// 多维变量类
// 运动单元
function Speed(x,y){
this.x = x;
this.y = y;
}
Speed.prtotype.run = function(){
console.log('运动');
}
//着色单元
function Color(cl){
this.color = cl;
}
Color.prototype.draw = function(){
console.log('绘制色彩');
}
function Ball(x,y,c){
this.speed = new Speed(x,y);
this.color = new Color(c);
}
Ball.prototype.init = function(){
this.speed.run();
this.color.draw();
}
var b = new Ball(10,20,'红色');
b.init();
网友评论