JavaScript中的方法重载
体验了下JavaScript中的面向对象的模式,这个让人偶尔非常郁闷的语言似乎不大支持方法重载。根据OOP多态性的概念,偶想了想,其实方法重载在JavaScript中也还是可以变通实现的。可以通过传递的方法参数的个数和类型来判断并调用不同的JS函数。比如下的一个创建菜单的类:
var myMenu = new JarulyMenu();
myMenu.Add();
myMenu.Add("Cmd1");
myMenu.Add("Cmd1", "Cmd2",……)
myMenu.Add(299, "Cmd");
以下仅是部分代码。还是比较简单的:)
var myMenu = new JarulyMenu();
myMenu.Add();
myMenu.Add("Cmd1");
myMenu.Add("Cmd1", "Cmd2",……)
myMenu.Add(299, "Cmd");
以下仅是部分代码。还是比较简单的:)
function JarulyMenu()
{
var obj = document.all;
this.Panel = "<div style=position:absolute;z-index:10;background:red><table border=1 id=JarulyMenuPanel><tr><td></td></tr></table></div>";
this.Width = "";
this.Event = getEventSource;
this.Add = setMenuItem;
this.Show = ShowAndHide;
}
function getEventSource()
{
return window.event.srcElement;
}
function getEventWidth()
{
return window.event.srcElement.offsetWidth;
}
function ShowAndHide()
{
var menuStatus = obj.JarulyMenuPanel.parentElement.style.display;
if (menuStatus == "")
{
obj.JarulyMenuPanel.parentElement.style.display = "none";
}
else
{
obj.JarulyMenuPanel.parentElement.style.display = "";
}
}
function setMenuItem()
{
//检测Panel是否已加载;没有则隐藏空菜单退出或者在此写入向document.body添加Panel的代码;
if (document.getElementById("JarulyMenuPanel")!="[object]")
{
alert("Error:不能加载菜单栏\n Design by FlaotPing");
return;
}
//方法重载setMenuItem() 或 setMenuItem(str1,str2, );
var numItem = setMenuItem.arguments.length;
if (numItem == 0)
{
//无数数则不显示菜单条;
return;
}
else
{
this.Width = getEventWidth();
var sCmd="";
for(i=0; i<numItem; i++)
{
sCmd += setMenuItem.arguments[i]+"\n";
}
obj.JarulyMenuPanel.rows(0).cells(0).innerHTML = sCmd;
obj.JarulyMenuPanel.width = this.Width;
obj.JarulyMenuPanel.parentElement.style.left = obj.menuBar.offsetLeft + this.Event().offsetLeft + this.Width - 8;
obj.JarulyMenuPanel.parentElement.style.top = parseInt(obj.menuBar.offsetTop) + parseInt(this.Event().clientTop) + parseInt(this.Event().height)+3;
this.Show();
obj.JarulyMenuPanel.onmouseout = this.Show;
}
}
{
var obj = document.all;
this.Panel = "<div style=position:absolute;z-index:10;background:red><table border=1 id=JarulyMenuPanel><tr><td></td></tr></table></div>";
this.Width = "";
this.Event = getEventSource;
this.Add = setMenuItem;
this.Show = ShowAndHide;
}
function getEventSource()
{
return window.event.srcElement;
}
function getEventWidth()
{
return window.event.srcElement.offsetWidth;
}
function ShowAndHide()
{
var menuStatus = obj.JarulyMenuPanel.parentElement.style.display;
if (menuStatus == "")
{
obj.JarulyMenuPanel.parentElement.style.display = "none";
}
else
{
obj.JarulyMenuPanel.parentElement.style.display = "";
}
}
function setMenuItem()
{
//检测Panel是否已加载;没有则隐藏空菜单退出或者在此写入向document.body添加Panel的代码;
if (document.getElementById("JarulyMenuPanel")!="[object]")
{
alert("Error:不能加载菜单栏\n Design by FlaotPing");
return;
}
//方法重载setMenuItem() 或 setMenuItem(str1,str2, );
var numItem = setMenuItem.arguments.length;
if (numItem == 0)
{
//无数数则不显示菜单条;
return;
}
else
{
this.Width = getEventWidth();
var sCmd="";
for(i=0; i<numItem; i++)
{
sCmd += setMenuItem.arguments[i]+"\n";
}
obj.JarulyMenuPanel.rows(0).cells(0).innerHTML = sCmd;
obj.JarulyMenuPanel.width = this.Width;
obj.JarulyMenuPanel.parentElement.style.left = obj.menuBar.offsetLeft + this.Event().offsetLeft + this.Width - 8;
obj.JarulyMenuPanel.parentElement.style.top = parseInt(obj.menuBar.offsetTop) + parseInt(this.Event().clientTop) + parseInt(this.Event().height)+3;
this.Show();
obj.JarulyMenuPanel.onmouseout = this.Show;
}
}