js prototype 理解

來源:互聯網
上載者:User

標籤:屬性   font   color   follow   ret   mic   var   子類   target   

簡單理解:prototype對象是實現物件導向的一個重要機制。每個函數也是一個對象,它們對應的類就是

function,每個函數對象都具有一個子物件prototype。Prototype 表示了該函數的原型,

prototype表示了一個類的屬性的集合。當通過new來產生一個類的對象時,prototype對象的屬

性就會成為執行個體化對象的屬性。 (類似反射出來的一樣)


ps.(在JS 裡面 全都是對象,  類Function是function的頂級超類,function執行個體化了叫對象,在function未執行個體定義的時候,其實這個定義也是屬於Function)

 
  1. <script>
  2. /*
  3. * 關於prototype,理解這個很有必要
  4. * 可以在類型上使用proptotype來為類型添加行為。這些行為只能在類型的執行個體上體現。
  5. * JS中允許的類型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String
  6. * 以後這樣分,沒有執行個體化的類稱為類型,執行個體化的類稱為對象執行個體簡稱執行個體
  7. */
  8. Object.prototype.name = "zhangsan";
  9. Object.prototype.nihao = function(){
  10. alert("i can method name is "+this.name);
  11. }
  12. var obj = new Object();
  13. obj.nihao();
  14. alert(obj.name);
  15. //在執行個體上不能使用prototype,否則會發生編譯錯誤
  16. obj.prototype.sex = "男";//error,無法給一個執行個體prototype
  17. var o = {
  18. name:"zhangsan"
  19. }
  20. o.prototype.age = 30;//error,無法給一個執行個體prototype
  21. //可以為類型定義“靜態”的屬性和方法,直接在類型上調用即可
  22. alert(Object.name);
  23. Object.nihao();
  24. //執行個體不能調用類型的靜態屬性或方法,否則發生對象未定義的錯誤。
  25. Object.class = "三年二班";//類靜態屬性
  26. var ob = new Object();
  27. alert(ob.class); //error 執行個體不能調用類型的靜態屬性和方法
  28. //可以在外部使用prototype為自訂的類型添加屬性和方法。
  29. function Mytest(){
  30. this.name = "zhangsan";
  31. this.age = 20;
  32. }
  33. Mytest.prototype.hello = function(){
  34. alert(this.name);
  35. }
  36. var m = new Mytest();
  37. m.hello();
  38. //在外部不能通過prototype改變自訂類型的屬性或方法。
  39. //該例子可以看到:調用的屬性和方法仍是最初定義的結果。
  40. Mytest.prototype.name = "lisi";
  41. var mm = new Mytest();
  42. alert(mm.name);
  43. //可以在對象執行個體上改變或增加屬性。(這個是肯定的)
  44. //也可以在對象上改變或增加方法。(和普遍的物件導向的概念不同)
  45. mm.name2 = "lisi";
  46. mm.hello = function(){
  47. alert(this.name2);
  48. }
  49. //mm.hello();
  50. //繼承,這個例子說明了一個類型如何從另一個類型繼承。
  51. function Mytest2(){}
  52. Mytest2.prototype = new Mytest;
  53. var m2 = new Mytest2();
  54. alert(m2.name);
  55. //這個例子說明了子類如何重寫父類的屬性或方法。
  56. Mytest2.prototype.name = "wangwu";
  57. Mytest2.prototype.hello = function(){
  58. alert(‘i can mytest2 extend Mytest save method hello‘);
  59. }
  60. var m3 = new Mytest2();
  61. m3.hello();
  62. //子類中的name屬性值不會被父類覆蓋
  63. function Mytest3(){
  64. this.name = "子類中的name屬性值不會被父類覆蓋";
  65. this.age = 20;
  66. }
  67. Mytest3.prototype = new Mytest();
  68. var m4 = new Mytest3();
  69. alert(m4.name);
  70. </script>


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.