宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应

[JS]js中prototype属性和应用说明附实例

2016-10-13 00:16:48 点击量:71 标签: 收藏本文

[JS]js中prototype属性和应用说明附实例

  1. <html> 
  2. <head> 
  3. <title>JS学习</title> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. </head> 
  6. <body> 
  7. js prototype 详解 
  8. <script> 
  9. //例子一 
  10. /*Object.prototype.Peoperty = 1; 
  11. Object.prototype.Method = function() 
  12. { 
  13. alert("this method 1"); 
  14. } 
  15. var obj = new Object(); 
  16. alert(obj.Peoperty); 
  17. obj.Method();*/ 
  18. //可以在类型上使用ptoptotype来为类型添加行为,这些行为只能在类型的实例上体现 
  19. //JS中允许的类型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String 
  20.  
  21.  
  22. //例子二 
  23. /*var obj = new Object(); 
  24. obj.prototype.Property = 1; 
  25. obj.prototype.Mothod = function() 
  26. { 
  27. alert("1"); 
  28. }*/ 
  29. //在实例上不能使用prototype,否则发生编译错误 
  30.  
  31. //例子三 
  32. /*Object.Property = 1; 
  33. Object.Method = function() 
  34. { 
  35. alert("this method"); 
  36. } 
  37. alert(Object.Property); 
  38. Object.Method();*/ 
  39. //可以为类型定义"静态"的属性和方法,直接在类开 
  40.  
  41. //例子四 
  42. /*//MyObject.Property = 1; 
  43. //只能使用Object关键字才行 
  44. Object.Property = 1; 
  45. Object.Method = function() 
  46. { 
  47. alert("this MyObject"); 
  48. } 
  49. //var obj = new MyOjbect(); 
  50. //alert(obj.Property); 
  51. //obj.Method(); 
  52. Object.Method();*/ 
  53. //Error 实例不能调用类型的静态属性或方法,否则发生对像未定义的错误 
  54.  
  55.  
  56. //实例五 
  57. /*function Aclass() 
  58. { 
  59. this.Property = 1; 
  60. this.Method = function() 
  61. { 
  62.  
  63. alert("Aclass Method"); 
  64. } 
  65. } 
  66. var obj = new Aclass(); 
  67. alert(obj.Property); 
  68. obj.Method();*/ 
  69. //这个例子演示了通常的在JavaScript中定义的一个类型的方法 
  70.  
  71.  
  72. //例子六 
  73. /*function Aclass() 
  74. { 
  75. this.Property = 1; 
  76. this.Method = function() 
  77. { 
  78. alert("Aclass Method"); 
  79. } 
  80. } 
  81. Aclass.prototype.Property2 = 2; 
  82. Aclass.prototype.Method2 = function() 
  83. { 
  84. alert("Aclass Method2"); 
  85. } 
  86. var obj = new Aclass(); 
  87. alert(obj.Property2); 
  88. obj.Method2();*/ 
  89. //可以在外部使用prototype为自定义的类型添加属性和方法 
  90.  
  91.  
  92. //例子七 
  93. /*function Aclass() 
  94. { 
  95. this.Peoperty = 11; 
  96. this.Method = function() 
  97. { 
  98. alert("this Method"); 
  99. } 
  100. } 
  101. Aclass.prototype.Peoperty = 22; 
  102. Aclass.prototype.Method = function() 
  103. { 
  104. alert("Aclass Method2"); 
  105. } 
  106. var obj = new Aclass(); 
  107. alert(obj.Property); //我测试的结果为: 属性是可以在外部进行修改的 
  108. obj.Method(); //但方法在外部修改不起作用*/ 
  109. //后面发现单词写错了 
  110. //在外部不能通过prototype改变自定义类型的属性和方法 
  111. //该例子可以看到,调用的属性和方法仍是最初定义的结果 
  112. //=================================================== 
  113.  
  114.  
  115. //例子八 
  116. /*function Aclass() 
  117. { 
  118. this.Peoperty = 11; 
  119. this.Method = function() 
  120. { 
  121. alert("this Method"); 
  122. } 
  123. } 
  124. var obj = new Aclass(); 
  125. obj.Peoperty = 22; 
  126. obj.Method = function() 
  127. { 
  128. alert("this Method2"); 
  129. } 
  130. alert(obj.Peoperty); 
  131. obj.Method();*/ 
  132. //可以在对像上改变属性 
  133. //也可以在对像上改变方法(和普通的面向对像的不同) 
  134.  
  135. //例子九 
  136. /*function Aclass() 
  137. { 
  138.  
  139. this.Property = 1; 
  140. this.Method = function() 
  141. { 
  142. alert("this Method"); 
  143. } 
  144. } 
  145. var obj = new Aclass(); 
  146. obj.Property2 = 22; 
  147. obj.Method2 = function() 
  148. { 
  149. alert("this Method2"); 
  150. } 
  151. alert(obj.Property2); 
  152. obj.Method2();*/ 
  153. //可以在对像上增加属性或方法 
  154.  
  155.  
  156. //例子十 
  157. /*function Aclass() 
  158. { 
  159. this.Property = 1; 
  160. this.Method = function() 
  161. { 
  162. alert("this Method"); 
  163. } 
  164. } 
  165.  
  166. function Aclass2() 
  167. { 
  168. this.Property2= 22; 
  169. this.Method2 = function() 
  170. { 
  171. alert("this Method2"); 
  172. } 
  173. } 
  174.  
  175. Aclass2.prototype = new Aclass(); //Aclass2继承Aclass类型 
  176. var obj = new Aclass2(); 
  177. alert(obj.Property); 
  178. obj.Method(); 
  179. alert(obj.Property2); 
  180. obj.Method2();*/ 
  181. //说明了一个类型如何从另一个类型继承 
  182.  
  183.  
  184. //例子十一 
  185. function Aclass() 
  186. { 
  187. this.Property = 1; 
  188. this.Method = function(){ 
  189. alert("this Method"); 
  190. } 
  191. } 
  192. function Aclass2() 
  193. { 
  194. this.Property2 = 22; 
  195. this.Method2 = function(){ 
  196. alert("this Method2"); 
  197. } 
  198. } 
  199. Aclass2.prototype = new Aclass(); 
  200. Aclass2.prototype.Property = 33; 
  201. Aclass2.Property.Method = function() 
  202. { 
  203. alert("this Method"); 
  204. } 
  205. alert(obj.Property); 
  206. obj.Method(); 
  207. //这个例子说明了子类如何重写父类的属性或方法 
  208.  
  209. //JavaScript能够实现的面向对像的特征有 
  210. //公有属性(public field) 
  211. //公有方法(public Method) 
  212. //私有属性(private field) 
  213. //私有方法(provate Method) 
  214. //方法重载(method overload) 
  215. //构造函数(constructor) 
  216. //事件(event) 
  217. //单一继承(single inherit) 
  218. //子类重写父类的属性或方法(override) 
  219. //静态属性或方法(static member) 
  220. </script> 
  221.  
  222. </body> 
  223. </html>