javascript原型的疑問

來源:互聯網
上載者:User

1.原型模型要求一個類型在一個時刻只能有一個原型(而一個執行個體在一個時刻可以有多個類型)。對於JavaScript來說,這個限制有兩層含義,第一是每個具體的JavaScript類型有且僅有一個原型(prototype),在預設的情況下,該原型是一個Object對象(注意不是Object類型)。第二是,這個類型的執行個體的所有類型,必須是滿足原型關係的類型鏈。

原型模型要求一個類型在一個時刻只能有一個原型的疑問.hmtl

<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><title>原型模型要求一個類型在一個時刻只能有一個原型的疑問</title></head><body><script><!--  function dwn(s)  {    document.write(s + "<br/>");  }  //定義Point2D對象  function Point2D(x,y)  {    this.x = x;    this.y = y;  }  Point2D.prototype.x = 0;  Point2D.prototype.y = 0;    //定義ColorPoint2D對象  function ColorPoint2D(x,y,c)  {    this.x = x;    this.y = y;  }    //ColorPoint2D以Point2D對象為原型  ColorPoint2D.prototype = new Point2D(90,90);  ColorPoint2D.prototype.x = 1;  ColorPoint2D.prototype.y = 1;    //構造一個ColorPoint2D對象  var cp = new ColorPoint2D(10,20,"red");  dwn(cp.x);  //10先尋找cp本身的屬性  delete cp.x;  dwn(cp.x);  //1 刪除後尋找上層原型鏈上的屬性  delete ColorPoint2D.prototype.x;  dwn(cp.x);  //0刪除後繼續尋找更上層原型鏈上的屬性  /*  * ColorPoint2D.prototype = new Point2D(90,90);  * ColorPoint2D.prototype.x = 1;  * ColorPoint2D.prototype.y = 1;  *原型模型要求一個類型在一個時刻只能有一個原型,ColorPoint2D的原型是new Point2D(90,90)對象,還是什麼  */--></script></body>
相關文章

聯繫我們

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