国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript中繼承用法實例分析

2024-05-06 16:20:26
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了JavaScript中繼承用法,以實例形式較為詳細的分析了javascript實現繼承的相關技巧,需要的朋友可以參考下

本文實例分析了JavaScript中繼承的用法。分享給大家供大家參考。具體如下:

 

 
  1. // define the Person Class 
  2. function Person() {} 
  3. Person.prototype.walk = function(){ 
  4. alert ('I am walking!'); 
  5. }; 
  6. Person.prototype.sayHello = function(){ 
  7. alert ('hello'); 
  8. }; 
  9. // define the Student class 
  10. function Student() { 
  11. // Call the parent constructor 
  12. Person.call(this); 
  13. // inherit Person 
  14. Student.prototype = new Person(); 
  15. // correct the constructor pointer because it points to Person 
  16. Student.prototype.constructor = Student; 
  17. // replace the sayHello method 
  18. Student.prototype.sayHello = function(){ 
  19. alert('hi, I am a student'); 
  20. // add sayGoodBye method 
  21. Student.prototype.sayGoodBye = function(){ 
  22. alert('goodBye'); 
  23. var student = new Student(); 
  24. student.sayHello(); 
  25. student.walk(); 
  26. student.sayGoodBye(); 
  27. // check inheritance 
  28. alert(student instanceof Person); // true  
  29. alert(student instanceof Student); // true 

希望本文所述對大家的javascript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 灵石县| 海宁市| 枝江市| 江陵县| 任丘市| 修水县| 中西区| 浮梁县| 文安县| 措美县| 黔西县| 靖远县| 图们市| 方山县| 本溪市| 陇川县| 龙山县| 河津市| 峨眉山市| 额尔古纳市| 集贤县| 班玛县| 惠来县| 南皮县| 通化市| 高邮市| 洞口县| 晋宁县| 香河县| 延庆县| 常州市| 邹城市| 天门市| 娄底市| 明溪县| 承德县| 洮南市| 通辽市| 承德县| 宝应县| 黎平县|