談談js中的prototype及prototype屬性解釋和常用方法,jsprototype

來源:互聯網
上載者:User

談談js中的prototype及prototype屬性解釋和常用方法,jsprototype

prototype是javascript中筆記難理解的一部分內容,下面通過幾個關鍵知識點給大家講解js中的prototype。具體內容請看下文詳情。

1 原型法設計模式

在.Net中可以使用clone()來實現原型法

原型法的主要思想是,現在有1個類A,我想要建立一個類B,這個類是以A為原型的,並且能進行擴充。我們稱B的原型為A。

2 javascript的方法可以分為三類:

a 類方法
b 對象方法
c 原型方法

例子:

function People(name){ this.name=name; //對象方法 this.Introduce=function(){ alert("My name is "+this.name); }}//類方法People.Run=function(){ alert("I can run");}//原型方法People.prototype.IntroduceChinese=function(){ alert("我的名字是"+this.name);}//測試var p1=new People("Windking");p1.Introduce();People.Run();p1.IntroduceChinese(); 

3 obj1.func.call(obj)方法

意思是將obj看成obj1,調用func方法

好了,下面一個一個問題解決:

prototype是什麼含義?

javascript中的每個對象都有prototype屬性,Javascript中對象的prototype屬性的解釋是:返回物件類型原型的引用。

A.prototype = new B();

理解prototype不應把它和繼承混淆。A的prototype為B的一個執行個體,可以理解A將B中的方法和屬性全部複製了一遍。A能使用B的方法和屬性。這裡強調的是複製而不是繼承。可以出現這種情況:A的prototype是B的執行個體,同時B的prototype也是A的執行個體。

先看一個實驗的例子:

function baseClass(){ this.showMsg = function() {  alert("baseClass::showMsg");  }}function extendClass(){}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); // 顯示baseClass::showMsg

我們首先定義了baseClass類,然後我們要定義extentClass,但是我們打算以baseClass的一個執行個體為原型,來複製的extendClass也同時包含showMsg這個對象方法。

extendClass.prototype = new baseClass()就可以閱讀為:extendClass是以baseClass的一個執行個體為原型複製建立的。

那麼就會有一個問題,如果extendClass中本身包含有一個與baseClass的方法同名的方法會怎麼樣?

下面是擴充實驗2:

function baseClass(){ this.showMsg = function() {  alert("baseClass::showMsg");  }}function extendClass(){ this.showMsg =function () {  alert("extendClass::showMsg"); }}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg();//顯示extendClass::showMsg

實驗證明:函數運行時會先去本體的函數中去找,如果找到則運行,找不到則去prototype中尋找函數。或者可以理解為prototype不會複製同名函數。

那麼又會有一個新的問題:

如果我想使用extendClass的一個執行個體instance調用baseClass的對象方法showMsg怎麼辦?

答案是可以使用call:

extendClass.prototype = new baseClass();var instance = new extendClass();var baseinstance = new baseClass();baseinstance.showMsg.call(instance);//顯示baseClass::showMsg

這裡的baseinstance.showMsg.call(instance);閱讀為“將instance當做baseinstance來調用,調用它的對象方法showMsg”

好了,這裡可能有人會問,為什麼不用baseClass.showMsg.call(instance);

這就是對象方法和類方法的區別,我們想調用的是baseClass的對象方法

最後,下面這個代碼如果理解清晰,那麼這篇文章說的就已經理解了:

<script type="text/javascript">function baseClass(){ this.showMsg = function() {  alert("baseClass::showMsg");  } this.baseShowMsg = function() {  alert("baseClass::baseShowMsg"); }}baseClass.showMsg = function(){ alert("baseClass::showMsg static");}function extendClass(){ this.showMsg =function () {  alert("extendClass::showMsg"); }}extendClass.showMsg = function(){ alert("extendClass::showMsg static")}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); //顯示extendClass::showMsginstance.baseShowMsg(); //顯示baseClass::baseShowMsginstance.showMsg(); //顯示extendClass::showMsgbaseClass.showMsg.call(instance);//顯示baseClass::showMsg staticvar baseinstance = new baseClass();baseinstance.showMsg.call(instance);//顯示baseClass::showMsg</script>

ps:js的Prototype屬性 解釋及常用方法

函數:原型

每一個建構函式都有一個屬性叫做原型(prototype,下面都不再翻譯,使用其原文)。這個屬性非常有用:為一個特定類聲明通用的變數或者函數。

prototype的定義

你不需要顯式地聲明一個prototype屬性,因為在每一個建構函式中都有它的存在。你可以看看下面的例子:

Example PT1
CODE:

function Test(){}alert(Test.prototype); // 輸出 "Object"

給prototype添加屬性

就如你在上面所看到的,prototype是一個對象,因此,你能夠給它添加屬性。你添加給prototype的屬性將會成為使用這個建構函式建立的對象的通用屬性。
例如,我下面有一個資料類型Fish,我想讓所有的魚都有這些屬性:livesIn="water"和price=20;為了實現這個,我可以給建構函式Fish的prototype添加那些屬性。

Example PT2

CODE:

function Fish(name, color){this.name=name;this.color=color;}Fish.prototype.livesIn="water";Fish.prototype.price=20;

接下來讓我們作幾條魚:

CODE:

var fish1=new Fish("mackarel", "gray");var fish2=new Fish("goldfish", "orange");var fish3=new Fish("salmon", "white");

再來看看魚都有哪些屬性:

CODE:

for (int i=1; i<=3; i++){var fish=eval_r("fish"+i); // 我只是取得指向這條魚的指標alert(fish.name+","+fish.color+","+fish.livesIn+","+fish.price);}

輸出應該是:

CODE:

"mackarel, gray, water, 20""goldfish, orange, water, 20""salmon, white water, 20"

你看到所有的魚都有屬性livesIn和price,我們甚至都沒有為每一條不同的魚特別聲明這些屬性。這時因為當一個對象被建立時,這個建構函式 將會把它的屬性prototype賦給新對象的內部屬性__proto__。這個__proto__被這個對象用來尋找它的屬性。

你也可以通過prototype來給所有對象添加共用的函數。這有一個好處:你不需要每次在構造一個對象的時候建立並初始化這個函數。為瞭解釋這一點,讓我們重新來看Example DT9並使用prototype來重寫它:

用prototype給對象添加函數

Example PT3

CODE:

function Employee(name, salary){this.name=name;    this.salary=salary;}Employee.prototype.getSalary=function getSalaryFunction(){return this.salary;}Employee.prototype.addSalary=function addSalaryFunction(addition){this.salary=this.salary+addition;}

 我們可以象通常那樣建立對象:

CODE:

var boss1=new Employee("Joan", 200000);var boss2=new Employee("Kim", 100000);var boss3=new Employee("Sam", 150000);

並驗證它:

CODE:

alert(boss1.getSalary()); // 輸出 200000alert(boss2.getSalary()); // 輸出 100000alert(boss3.getSalary()); // 輸出 150000

這裡有一個圖示來說明prototype是如何工作的。這個對象的每一個執行個體(boss1, boss2, boss3)都有一個內部屬性叫做__proto__,這個屬性指向了它的構造器(Employee)的屬性prototype。當你執行 getSalary或者addSalary的時候,這個對象會在它的__proto__找到並執行這個代碼。注意這點:這裡並沒有代碼的複製(和 Example DT8的圖表作一下對比)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.