//定義一個類
function TestClass(name)
{
//初始化時總數+1
TestClass.prototype.Count++;
//定義一個屬性,並設定預設值
this.Name = name || "jxh";
//定義一個靜態類屬性
TestClass.prototype.Current.InternalName = this.Name;
//定義一個對象屬性
this.internalClass = new InternalClass(this.Name);
//定義一個方法
this.GetFormatName = function()
{
var fname = this.internalClass.FormatName();
return fname;
}
}
//為TestClass類定義一個靜態屬性
TestClass.prototype.Count = 0;
//為TestClass類定義一個靜態類屬性
TestClass.prototype.Current = new InternalClass("沒有執行個體");
//內部類
function InternalClass(name)
{
this.InternalName = name;
}
對於靜態屬性的訪問,除了 [類名.prototype.屬性名稱] 的訪問方式外,直接用 [對象名.屬性名稱] 也能訪問,但由於 [對象名.屬性名稱] 的訪問方式如果對屬性做了修改的話, [對象名.屬性名稱] 將不在和 [類名.prototype.屬性名稱] 的值一致,可能會引起誤會,所以建議對靜態屬性只使用[類名.prototype.屬性名稱] 一種方式去訪問。<script language = "javascript">
function Click_Event()
{
alert("沒有執行個體對象");
alert("TestClass.prototype.Count:" + TestClass.prototype.Count);
alert("TestClass.prototype.Current:" + TestClass.prototype.Current.InternalName);
var testClass1 = new TestClass();
alert("執行個體1個對象");
alert("TestClass.prototype.Count:" + TestClass.prototype.Count);
alert("TestClass.prototype.Current:" + TestClass.prototype.Current.InternalName);
var testClass2 = new TestClass("jxhwei");
alert("執行個體2個對象");
alert("TestClass.prototype.Count:" + TestClass.prototype.Count);
alert("TestClass.prototype.Current:" + TestClass.prototype.Current.InternalName);
}
</script>