Javascript乱弹设计模式系列(5) - 命令模式(Command)
2010-09-14 13:38:52 来源:WEB开发网Menu类的实现如下:
functionMenu(text,title,href){
this.menuComponents=newArray();
this.text=text;
this.title=title;
this.href=href;
Interface.registerImplements(this,MenuComponent);
}
Menu.prototype={
getElement:function(){
if(this.menuComponents.length==0)
{
thrownewError(this.text+"菜单下没有子菜单");
}
varliElement=document.createElement("li");
liElement.className="Menu-WithChildren";
liElement.title=this.title;
varanchor=document.createElement("a");
anchor.className="Menu-Link";
anchor.href=this.href;
liElement.appendChild(anchor);
anchor.innerHTML=this.text;
varulElement=document.createElement("ul");
liElement.appendChild(ulElement);
for(vari=0,len=this.menuComponents.length;i<len;i++)
{
ulElement.appendChild(this.menuComponents[i].getElement());
}
returnliElement;
},
add:function(component){
this.menuComponents.push(component);
},
remove:function(component){
for(vari=0,len=this.menuComponents.length;i<len;i++)
{
if(this.menuComponents[i]==component)
{
this.menuComponents.splice(i,1);
break;
}
}
},
removeAt:function(index){
if(this.menuComponents.length<=index)
{
this.menuComponents.splice(index,1);
}
else
{
thrownewError("索引操作数组超过上限");
}
}
}
Tags:Javascript 乱弹 设计模式
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接