不論是用java,還是c#,又或是vb,構建一個對象都很簡單,都可以采用 classobj =new classobj()的方法構造一個類,然后使用其中的屬性以及方法,其實javascript也是一樣可以實現的。
示例:建立一個js文件,定義一個對象 EmcObj
1>定義構造函數
示例:建立一個js文件,定義一個對象 EmcObj
1>定義構造函數
EmcObj=function(w,h)
{
this.xWidth=w;
this.yHeight=h; //this 表類私有變量
}
{
this.xWidth=w;
this.yHeight=h; //this 表類私有變量
}
2>給類定義方法
EmcObj.prototype._Init=function()
{
this.config={
render : "AUTO",
linkType : "M",
linkColor : "blue",
nodeColor : "#CCCCFF",
};
//獲取此屬性值用:this.config.nodeColor
}
{
this.config={
render : "AUTO",
linkType : "M",
linkColor : "blue",
nodeColor : "#CCCCFF",
};
//獲取此屬性值用:this.config.nodeColor
}
ECOTree.prototype.toString = function () {
var sl = [];
sl.push("<div name=abc>");
sl.push("</div>");
return sl.join('');
}
var sl = [];
sl.push("<div name=abc>");
sl.push("</div>");
return sl.join('');
}
3>定義屬性
this.config.name="aa";//這樣就行了
調用方法
var emc
function CreateEmcObj()
{
emc=new EmcObj(100,100);
document.write(emc.tostring());
}
function CreateInit()
{
emc._init();
}
function CreateEmcObj()
{
emc=new EmcObj(100,100);
document.write(emc.tostring());
}
function CreateInit()
{
emc._init();
}
ok