js物件導向-prototype

來源:互聯網
上載者:User

標籤:new   function   ati   對話方塊   class   object   prototype   obj   對話   

  在js中每個其實也是一個對象,他們對應的類是Function,每個函數對象都有一個子物件prototype,這個子物件表示了一個類的成員集合,當通過new來執行個體化一個對象

時,prototype中的成員都會成為執行個體化對象的成員。如下例子:

function class1(){}

class1.prototype.method=function(){alert("測試");};

class1 cl=new class1();

cl.method();

會彈出提示框“測試”。

由此可以發現,prototype的成員成了cl的成員。

 

但是需要注意的是class1 cl=new class1();這句代碼要寫在class1.prototype.method之後。

prototype對構造器的引用:

function class1()

{

alert("我是建構函式");

}

class1.prototype.constuctor();

 

在舉個例子:

function b(){
this.innerobject="cc";
}
b.staticP="1";
b.prototype.methoned=function () {
};
var newb=new b();
//利用反射來便利newb的成員
for(var j in newb)
{
alert(j+":"+newb[j]);
}
會彈出對話方塊:
innerobject:cc和
methoned:function(){}
證明,prototype的成員成了b
的成員。
over!!!


 

js物件導向-prototype

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.