//摘自http://bbs.51js.com
<SCRIPT LANGUAGE="JavaScript">
<!--
function Base( v_sBaseName )
{
this.BaseName = v_sBaseName
this.BaseMethod = BaseMethod;
function BaseMethod( v_sStr )
{
alert("BaseName: " + this.BaseName + "\n" + "ExtendStr: " + v_sStr);
}
}
function Son( v_sName )
{
this.Name = v_sName
this.BaseName = this.Name;
this.Method = Method;
function Method( v_sStr )
{
alert("Name: " + this.Name + "\n" + "ExtendStr: " + v_sStr);
}
}
Son.prototype = new Base();
var O = new Son("初始化字串")
O.Method("Method附加字串");
O.BaseMethod("BaseMethod附加字串");
//-->
</SCRIPT>
SCRIPT LANGUAGE="JavaScript">
<!--
function Base( v_sBaseName )
{
this.BaseName = v_sBaseName
this.BaseMethod = BaseMethod;
function BaseMethod( v_sStr )
{
alert("BaseName: " + this.BaseName + "\n" + "ExtendStr: " + v_sStr);
}
}
function Son( v_sName )
{
Base.call(this, v_sName)
this.Name = v_sName
this.Method = Method;
function Method( v_sStr )
{
alert("Name: " + this.Name + "\n" + "ExtendStr: " + v_sStr);
}
}
var O = new Son("初始化字串")
O.Method("Method附加字串");
O.BaseMethod("BaseMethod附加字串");
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Base( v_sBaseName )
{
this.BaseName = v_sBaseName
this.BaseMethod = BaseMethod;
function BaseMethod( v_sStr )
{
alert("BaseName: " + this.BaseName + "\n" + "ExtendStr: " + v_sStr);
}
}
function Son( v_sName )
{
Base.call(this, v_sName)
this.base = new Base(v_sName) //建立一個基對象實例以便重載後可以調用基對象的函數
this.Name = v_sName
this.Method = Method;
function Method( v_sStr )
{
alert("Name: " + this.Name + "\n" + "ExtendStr: " + v_sStr);
}
this.BaseMethod = BaseMethod;
function BaseMethod( v_sStr )
{
alert("Override BaseName: " + this.BaseName + "\n" + "Override ExtendStr: " + v_sStr);
}
}
var O = new Son("初始化字串")
O.Method("Method附加字串");
O.BaseMethod("重載後的BaseMethod附加字串");
O.base.BaseMethod("BaseMethod附加字串");
//-->
</SCRIPT>
<script>
String.prototype.defaultText="這是String對象的默認值,也是一個靜態屬性";
var sss=new String();
alert(sss.defaultText);//任何字符傳都有這個屬性了
alert("所有的字符串都會有defaultText這個默認值屬性:"+"所有的字符串都會有defaultText這個默認值屬性".defaultText)
function b()
{
alert(String.prototype.defaultText);
b1.style.display="block";
}
function a()
{
delete String.prototype.defaultText;
}
</script>
<button onclick="b()">顯示String對象的defaultText屬性</button>
<button id=b1 style="display:none" onclick="a()">刪掉String對象的defaultText屬性</button>