<
SCRIPT?LANGUAGE?
=
?
"
JavaScript
"
>
?
<!--
?

?Object.extend??
=
???function?(destination,?source)???
{

???
for
??(property??in??source)???
{
????destination[property]??
=
??source[property];
??}
?
???
return
??destination;
}
?
?

??function??Man()??
{
?????
this
?.name?
=
?
'
zkj
'
;
}
?
Man.prototype.type?
=
?
'
男人
'
;

Man.prototype.getType?
=
?function?()??
{
?????
return
???
this
?.type;
}
?

??function??Woman()??
{}
?
?
Object.extend(Woman.prototype,Man.prototype);
?var??man?
=
?
new
??Man();
?var??woman?
=
?
new
??Woman();
alert(man.getType());
alert(man.name);
alert(woman.getType());
alert(woman.name);
//
?-->?
</
SCRIPT
>
?
我只能說javascript的繼承是模擬實現的。和java,c++中是不同的。是依靠prototype實現的。
我個人從來不用javascript的“繼承”,始終認為javascript實現的繼承不是真正的繼承??赡苁鞘躩ava”毒害“夠深。
在javascript中,我把繼承分為兩類: 類繼承,對象繼承。
(1)、prototype.js中的繼承prototype.js中用如下代碼實現繼承。我認為只是個屬性拷貝過程。

Object.extend??=???function?(destination,?source)???
{

???for??(property??in??source)???
{
????destination[property]??=??source[property];
??}?
???return??destination;
}?//Object.prototype.extend 感覺這句話沒必要,太模仿java了,想讓對象實例直接繼承。
???
a、prototype.js中的類繼承
prototype.js??1.3?.?1?

?????String.prototype.extend(??
{

??????stripTags:??function?()???
{
???????return???this?.replace(?/<?\?/??[?^>?]?+>/?gi,?'');
????}?,


????escapeHTML:??function?()???
{
???????var??div??=??document.createElement('div');
???????var??text??=??document.createTextNode(?this?);
??????div.appendChild(text);
???????return??div.innerHTML;
????}?,


????unescapeHTML:??function?()???
{
???????var??div??=??document.createElement('div');
??????div.innerHTML??=???this?.stripTags();
???????return??div.childNodes[?0?].nodeValue;
????}?
????}?);??? 我把這類型的繼承叫做類繼承,直接把你自己寫的對象屬性拷貝到原型對象中去。
<SCRIPT?LANGUAGE?=?"JavaScript">?
<!--?

?Object.extend??=???function?(destination,?source)???
{

???for??(property??in??source)???
{
????destination[property]??=??source[property];
??}?
???return??destination;
}?
?

??function??Man()??
{
?????this?.name?=?'zkj';
}?
Man.prototype.type?=?'男人';

Man.prototype.getType?=?function?()??
{
?????return???this?.type;
}?

??function??Woman()??
{}?
?
Object.extend(Woman.prototype,Man.prototype);
?var??man?=?new??Man();
?var??woman?=?new??Woman();
alert(man.getType());
alert(man.name);
alert(woman.getType());
alert(woman.name);
//?-->?
</SCRIPT>?看了以上代碼,可能你會明白。直接拷貝類的原型對象確實可以實現某種概念上的繼承。
但要注意:在繼承體系中,Man的原型對象屬性方法最好不要用Man的實例屬性(name),因為可能Woman中并沒有定義實例屬性name;也最好不要用Man)原型對象屬性字段(type),雖然type也被拷貝過來了,但值還是”男人“。
雖然有解決辦法,但javascript沒有很好的語法檢查工具,你用prototype.js的類繼承時小心處理。
b、prototype.js中的對象繼承
prototype.js 1.3.1
this.options = {
????? method:?????? 'post',
????? asynchronous: true,
????? parameters:?? ''
??? }.extend(options || {});
這個應用比較簡單,典型的對象之間屬性拷貝覆蓋。
總結:關于prototype.js中繼承的實現,我們當成javascript對象的屬性拷貝可能在應用中更好理解。建議大家仔細讀讀prototype.js代碼可能體會更深。模仿prototype.js中extend的應用。感覺var Insertion = new Object(); 的實現比較經典。
(2)、dojo-0.2.0-ajax中的繼承

dojo.inherits??=???function?(subclass,?superclass)??
{

??if?(?typeof??superclass??!=??'?function?')??
{?
??dojo.raise(?"?superclass:??"?+?superclass?+?"??borken?"?);
?}?
?subclass.prototype??=???new??superclass();
?subclass.prototype.constructor??=??subclass;
?subclass.superclass??=??superclass.prototype;
??//??DEPRICATED:?super?is?a?reserved?word,?use?'superclass'?
??subclass['super']??=??superclass.prototype;
?????}?dojo的繼承實現比較正統,也是《javascript權威指南》中的實現方法。注意最后一句代碼可以實現子類訪問父類原型對象的方法。
<SCRIPT?LANGUAGE?=?"JavaScript">?
?<!--?
?

??function??Man()??
{
?????this?.name?=?'zkj';
}?
Man.prototype.type?=?'男人';

Man.prototype.getType?=?function?()??
{
?????return???this?.type;
}?

??function??Woman()??
{}?
?
Woman.prototype??=???new??Man();
Woman.prototype.constructor??=??Woman;
Woman.superclass??=??Man.prototype;
?//??DEPRICATED:?super?is?a?reserved?word,?use?'superclass'?
?Woman['super']??=??Man.prototype;
Woman.prototype.type?=?'女人';

?var??man?=?new??Man();
?var??woman?=?new??Woman();
alert(man.getType());
alert(man.name);
alert(woman.getType());
alert(Woman.superclass.getType());
alert(woman.name);

//?-->?
</SCRIPT>?看看代碼,感覺混亂。
dojo一直沒時間仔細讀讀代碼。這部分詳細討論待續。
(3)、總結
關于javascript的繼承實現,建議一般不要使用,感覺很亂,代碼可讀性較差。一般也沒有使用必要。詳細在《9、javascript對象使用指南》中討論。