js function,prototype,sub.
Ojbect 和Function 與普通函數(shù)和實例對象
1.實例對象的proto 指向構(gòu)造函數(shù)的原型對象
2.實例對象的proto 指向Ojbect的原型
3.所有函數(shù)的proto 都指向Function的原型
function定義的對象有一個prototype屬性,使用new生成的對象就沒有這個prototype屬性。
prototype屬性又指向了一個prototype對象,注意prototype屬性與prototype對象是兩個不同的東西,要注意區(qū)別。在prototype對象中又有一個constructor屬性,這個constructor屬性同樣指向一個constructor對象,而這個constructor對象恰恰就是這個function函數(shù)本身。
有點頭暈,看下圖吧:
我們接著看代碼:
代碼 function Person(name)
{
this.name=name;
this.showMe=function()
{
alert(this.name);
}
};
Person.prototype.from=function()
{
alert('I come from prototype.');
}
var one=new Person('js');
one.showMe();//js,這個結(jié)果沒有什么好奇怪的
one.from();//I come from prototype.,這個結(jié)果有一點奇怪吧
要解釋這個結(jié)果就要仔細研究一下new這個操作符了.var one=new Person('js');這個語句執(zhí)行的過程可以分成下面的語句:
var one={};
Person.call(one,'js');
按照《悟透javascript》書中說的,new形式創(chuàng)建對象的過程實際上可以分為三步:
第一步是建立一個新對象(叫A吧);
第二步將該對象(A)內(nèi)置的原型對象設(shè)置為構(gòu)造函數(shù)(就是Person)prototype 屬性引用的那個原型對象;
第三步就是將該對象(A)作為this 參數(shù)調(diào)用構(gòu)造函數(shù)(就是Person),完成成員設(shè)置等初始化工作。
其中第二步中出現(xiàn)了一個新名詞就是內(nèi)置的原型對象,注意這個新名詞跟prototype對象不是一回事,為了區(qū)別我叫它inobj,inobj就指向了函數(shù)Person的prototype對象。在person的prototype對象中出現(xiàn)的任何屬性或者函數(shù)都可以在one對象中直接使用,這個就是javascript中的原型繼承了。
又頭暈了,上圖吧!
這樣one對象通過內(nèi)置的原型對象inobj就可以直接訪問Person的prototype對象中的任何屬性與方法了。這也就解釋了上面的代碼中為什么one可以訪問form函數(shù)了。因為prototype對象中有一個constructor屬性,那么one也可以直接訪問constructor屬性。
__proto__ is inobj????
繼承是如何實現(xiàn)的。
代碼 function Person(name)
{
this.name=name;
this.showMe=function()
{
alert(this.name);
}
};
Person.prototype.from=function()
{
alert('I come from prototype.');
}
function SubPerson()
{
}
SubPerson.prototype=new Person();
var subOne=new SubPerson();
subOne.from();//I come from prototype.
alert(subOne.constructor);//function Person(name) {...};
alert(SubPerson.prototype.constructor);//function Person(name) {...};
繼承的實現(xiàn)很簡單,只需要把子類的prototype設(shè)置為父類的一個(實例化)對象即可。注意這里說的可是對象哦!
那么通過prototype屬性實現(xiàn)繼承的原理是什么呢?還是先看圖形說明,然后編寫代碼進行驗證。
注意:紅色的方框就是把子類與父類鏈接起來的地方。這個就應(yīng)該是傳說中的prototype鏈了吧。下面有代碼進行驗證。
代碼 function Person(name)
{
this.name=name;
this.showMe=function()
{
alert(this.name);
}
};
Person.prototype.from=function()
{
alert('I come from prototype.');
}
var father=new Person('js');//為了下面演示使用showMe方法,采用了js參數(shù),實際多采用無參數(shù)
alert(father.constructor);//查看構(gòu)造函數(shù),結(jié)果是:function Person(name) {...};
function SubPer()
{
}
SubPer.prototype=father;//注意這里 ////在這里SubPer.prototype就指向了father對應(yīng)的對象
SubPer.prototype.constructor=SubPer; ////
//這行修改了SubPer.prototype.constructor,其實也給father對象添加了一個constructor屬性,此時father.prototype.constructor還是Person,所以father.constructor訪問的時候先是找到了自己constructor的屬性,就不再去原型里面找
var son=new SubPer();
son.showMe();//js
son.from();//I come from prototype.
alert(father.constructor);//function SubPer(){...}
alert(son.constructor);//function SubPer(){...}
alert(SubPer.prototype.constructor);//function SubPer(){...}
根據(jù)上圖的prototype鏈,還有代碼的結(jié)果,我想應(yīng)該明白為什么使用prototype能夠?qū)崿F(xiàn)
JS中的繼承了吧。
總結(jié)
以上是生活随笔為你收集整理的js function,prototype,sub.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF 自定义 ImageButton
- 下一篇: 使用Spring Boot创建docke