javascript 原型模式實(shí)現(xiàn)OOP的再研究
更新時(shí)間:2009年04月09日 18:15:13 作者:
目前網(wǎng)絡(luò)上有關(guān)javascript實(shí)現(xiàn)OOP模式的方案基本上都是prototype模式,一般性的示例代碼如下
復(fù)制代碼 代碼如下:
復(fù)制代碼 代碼如下:
function A()
{
this.v1 = 10;
}
A.prototype.print = function()
{
alert(this.v1);
}
function B()
{
}
B.prototype = new A();
new B().print();
運(yùn)行這段代碼輸出是10,看起來好像是類B繼承了類A的方法print,并產(chǎn)生了正確的輸出,實(shí)際上的執(zhí)行流程是在類B生成的對(duì)象中,不能直接得到方法print,于是在它的prototype屬性中查找對(duì)應(yīng)的方法,這個(gè)方案的出發(fā)點(diǎn)很好,類B中存在就調(diào)用類B的,否則調(diào)用它的prototype屬性(類A)中的同名方法。然而有時(shí)候我們需要子類的方法調(diào)用父類同名的方法,比如類B改為
復(fù)制代碼 代碼如下:
function B()
{
this.v2 = 15;
}
B.prototype = new A();
B.prototype.print = function()
{
this.prototype.print.call(this);
alert(this.v2);
}
new B().print();
其中,this.prototype.print就是類A對(duì)應(yīng)的print方法,輸出是10和15,好像解決了問題,實(shí)際上不然,我們?cè)倮^承一層
復(fù)制代碼 代碼如下:
function C()
{
this.v3 = 20;
}
C.prototype = new B();
C.prototype.print = function()
{
this.prototype.print.call(this);
alert(this.v3);
}
new C().print();
我們期待的輸出依次是10, 15, 20, 但是很不幸,這樣寫的結(jié)果是系統(tǒng)會(huì)陷入死循環(huán)
因?yàn)樵趫?zhí)行這個(gè)方法時(shí),
復(fù)制代碼 代碼如下:
C.prototype.print = function()
{
this.prototype.print.call(this);
alert(this.v3);
}
將會(huì)循環(huán)的調(diào)用以下方法,直到堆棧溢出
復(fù)制代碼 代碼如下:
B.prototype.print = function()
{
this.prototype.print.call(this);
alert(this.v2);
}
正確的寫法此時(shí)應(yīng)該變?yōu)?
復(fù)制代碼 代碼如下:
B.prototype.print = function()
{
A.prototype.print.call(this);
alert(this.v3);
}
C.prototype.print = function()
{
B.prototype.print.call(this);
alert(this.v3);
}
但是在繼承關(guān)系發(fā)生了改變的情況下,需要改動(dòng)相當(dāng)多的對(duì)父類的引用,這也不是最佳的辦法,在實(shí)際應(yīng)用中,可以考慮使用_super來代替父類的名稱,_this來代替自身的名稱,然后用一個(gè)標(biāo)準(zhǔn)的方法將他們替換成[super].prototype或者[this].prototype,從而沒有歧義的調(diào)用指定的方法,這才是javascript的OOP的真正解決方案,相關(guān)的代碼如下:
復(fù)制代碼 代碼如下:
/*
在使用OOP繼承體系時(shí), 首先要定義類, 最后執(zhí)行extendsOf初始化類, 使用_super引用父類, 如, 使用_this引用本身的方法,
例如:
function Extend2()
{
_super();
}
Extend2.prototype.setValue = function(value)
{
_super.setValue(value);
alert("Extend2:" + value);
}
Extend2.extendsOf(Extend1);
類繼承樹的根為Object. 注意: 所有使用了轉(zhuǎn)義的成員函數(shù)都必須定義在extendsOf方法調(diào)用之前.
對(duì)象可以設(shè)定一個(gè)自動(dòng)運(yùn)行的初始化代碼, 以下劃線開頭, 名稱與對(duì)象名稱相同, 如
Object._Object = function() {...}
如果對(duì)象的初始化代碼不存在, 將自動(dòng)尋找父對(duì)象的初始化代碼, 直到全部查找完畢
復(fù)制代碼 代碼如下:
Function.FRBlock = / *("([^"^\\]|\\")*"|'([^'^\\]|\\')*'|\/([^\/^\\]|\\.)*\/) */;
Function.FRSpace = /\s+/g;
Function.FRSign = / ?(^|;|:|<|>|\?|,|\.|\/|\{|\}|\[|\]|\-|\+|\=|\(|\)|\*|\^|\%|\|) ?/g;
Function.FRRefer = /_(super|this)(\.[^(]+)?\(([^\)]*)\)/;
Function.prototype.FCompile = function(name)
{
//檢查是類的構(gòu)造函數(shù)還是類的屬性, name參數(shù)為空表示是構(gòu)造函數(shù)
if (name)
{
//類的屬性不是函數(shù)實(shí)現(xiàn), 直接賦值到子類后退出
if (typeof this.prototype[name] != "function")
{
window[this.FClassName].prototype[name] = this.prototype[name];
return;
}
var s = this.prototype[name].toString();
}
else
{
var s = this.toString();
}
var b = "";
var r;
//過濾空白字符
while (r = Function.FRBlock.exec(s))
{
s = RegExp.rightContext;
b += RegExp.leftContext.replace(Function.FRSpace, " ").replace(Function.FRSign, "$1") + r[1];
}
b += s.replace(Function.FRSpace, " ").replace(Function.FRSign, "$1");
var i = b.indexOf("(");
var j = b.indexOf(")", i);
if (!name)
{
this.FClassName = b.substring(9, i);
}
var cn = this.FClassName;
var arg = b.substring(i + 1, j);
s = b.substring(j + 2, b.length - 1);
b = "";
//進(jìn)行調(diào)用轉(zhuǎn)義, 將_super,_this替換為指定的方法
for (var n = 0; r = Function.FRRefer.exec(s); n++)
{
if (r[2])
{
if (!name && !n)
{
b = this.FSuperClass.FClassName + ".apply(this,arguments);";
}
r[2] = ".prototype" + r[2];
}
else if (r[1] == "this")
{
//JS函數(shù)不區(qū)分參數(shù)的差異, 構(gòu)造函數(shù)不允許遞歸調(diào)用自身
throw "Constructor call mustn't be \"_this();\" in a constructor";
}
else if (name || RegExp.leftContext)
{
throw "Constructor call must be the first statement in a constructor";
}
else
{
r[2] = "";
}
s = RegExp.rightContext;
b += RegExp.leftContext + (r[1] == "this" ? cn : this.FSuperClass.FClassName) + r[2] + (r[3] ? ".call(this," + r[3] + ")" : ".apply(this,arguments)");
}
if (n)
{
b += s;
}
else if (name)
{
//沒有針對(duì)_this,_super的調(diào)用, 不用編譯
window[cn].prototype[name] = this.prototype[name];
return;
}
else
{
//沒有對(duì)父類構(gòu)造函數(shù)的調(diào)用時(shí), 自動(dòng)添加
b = this.FSuperClass.FClassName + ".apply(this,arguments);" + s;
}
//編譯結(jié)果賦值
if (name)
{
eval(cn + ".prototype." + name + "=function(" + arg + "){" + b + "}");
}
else
{
eval(cn + "=function(" + arg + "){" + b + ";if(this.constructor==" + cn + ")" + cn + "._" + cn + ".apply(this,arguments);}");
window[cn].FClassName = cn;
}
}
Function.prototype.extendsOf = function(superClass)
{
this.FSuperClass = superClass;
//編譯類的全部函數(shù)
this.FCompile();
for (var name in this.prototype)
{
this.FCompile(name);
}
var clazz = window[this.FClassName];
clazz.FSuperClass = superClass;
//復(fù)制父類中子類沒有實(shí)現(xiàn)的函數(shù)和屬性
var prototype = clazz.prototype;
for (var name in superClass.prototype)
{
if (!prototype[name])
{
prototype[name] = superClass.prototype[name];
}
}
//復(fù)制初始化方法, 形式如Object._Object
for (var c = this; ; c = c.FSuperClass)
{
if (c["_" + c.FClassName])
{
clazz["_" + clazz.FClassName] = c["_" + c.FClassName];
return;
}
}
}
/*
內(nèi)置Object類為OOP提供的支持
*/
Object.FClassName = "Object";
Object._Object = Function.Instance;
Object.prototype.instanceOf = function(clazz)
{
for (var c = this.constructor; c; c = c.FSuperClass)
{
if (c === clazz)
{
return true;
}
}
return false;
}
您可能感興趣的文章:
- JS面向?qū)ο蠡A(chǔ)講解(工廠模式、構(gòu)造函數(shù)模式、原型模式、混合模式、動(dòng)態(tài)原型模式)
- js面向?qū)ο笾R妱?chuàng)建對(duì)象的幾種方式(工廠模式、構(gòu)造函數(shù)模式、原型模式)
- JavaScript面向?qū)ο蟪绦蛟O(shè)計(jì)三 原型模式(上)
- JavaScript設(shè)計(jì)模式之原型模式(Object.create與prototype)介紹
- javascript設(shè)計(jì)模式 – 抽象工廠模式原理與應(yīng)用實(shí)例分析
- javascript設(shè)計(jì)模式 – 工廠模式原理與應(yīng)用實(shí)例分析
- javascript設(shè)計(jì)模式 – 簡單工廠模式原理與應(yīng)用實(shí)例分析
- javascript設(shè)計(jì)模式 – 單例模式原理與應(yīng)用實(shí)例分析
- javascript 設(shè)計(jì)模式之享元模式原理與應(yīng)用詳解
- javascript 設(shè)計(jì)模式之組合模式原理與應(yīng)用詳解
- javascript設(shè)計(jì)模式 – 原型模式原理與應(yīng)用實(shí)例分析
相關(guān)文章
JS實(shí)現(xiàn)網(wǎng)頁搶購功能(觸發(fā),終止腳本)
小編通過一個(gè)網(wǎng)頁式的搶購功能的實(shí)現(xiàn)給大家講解一下JS如何觸發(fā)和終止腳本來完成這個(gè)任務(wù)。2017-11-11JavaScript實(shí)現(xiàn)簡單計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡單計(jì)時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06基于 webpack2 實(shí)現(xiàn)的多入口項(xiàng)目腳手架詳解
這篇文章主要給大家介紹了基于 webpack2 實(shí)現(xiàn)的多入口項(xiàng)目腳手架的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06