<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>
注意观察所谓“继承对象”的 this.Prototype 这个变量(一开始我没注意看,以为是关键字……害我苦找资料)
this.Prototype = new JSObject(); // 注意:这可不是小写的prototype
this.Prototype.Speak = function(s){.......}
在构造对象后直接反馈 return this.Prototype 其实这个就是 JSPObject 对象,
并不是 JSHuman 对象,所以 var o = new JSHuman();
o 这个实例是无法访问 JSHuman 的成员的
<script language=jscript>
//westfly原创
//不要问我有什么用,当你某一天需要用到时能想起来就可以了
function JSObject()
{
this._Name = "JSObject";
this.Set_Name = function(Value){
this._Name = Value;
}
this.Get_Name = function(){
return this._Name;
}
this.Speak = function(){alert("wangwang")}
}
function JSHuman() // extends JSObject
{
this.prototype = new JSObject(); // 注意:这可不是小写的prototype
this.prototype.Speak = function(s){
alert(s);
}
return this.prototype;
}
var o = new JSHuman();
o.Set_Name("westfly");
o.Speak(o.Get_Name());
</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>
stroll,方法的確繼承過來了,但我在Son對象的搆造函數裏重寫了BaseMethod()
事實上O.BaseMethod()這樣調用已經不是Base對象的BaseMethod()方法了,而是Son對象的BaseMethod()方法,所以我把Base的一個實例儅作Son的成員base,使用
Son實例.base.BaseMethod()
的格式來引用Base的BaseMethod()方法
另外,我喜懽這句:Base.apply(this, arguments)
