A test of javascript's inheritance

来源:互联网 发布:网络公关营销公司 编辑:程序博客网 时间:2024/04/29 14:14

This is a simple test of javascript's inheritance,you also can change the "apply" method to "call". the result is same.

 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Javascript Class extend test</title>
  5. <meta name="Generator" content="EditPlus">
  6. <meta name="Author" content="^_^-Stefli,^_^-Beluga">
  7. <meta name="Keywords" content="">
  8. <meta name="Description" content="">
  9. </head>
  10. <body>
  11. <script>
  12. function BaseClass() {
  13.     this.field = "field";
  14.     this.getMethod = function() {
  15.         return "getMethod";
  16.     }
  17.     this.getOverrideMethod = function() {
  18.         return "getOverrideMethod";
  19.     }
  20. }
  21. function SubClass() {
  22.     BaseClass.call(this);
  23.     this.f = "f";
  24.     this.getM = function() {
  25.         return "getM";
  26.     }
  27. }
  28. function SubSubClass() {
  29.     SubClass.call(this);
  30.     this.subf = "subf";
  31.     this.getSubM = function() {
  32.         return "getSubM";
  33.     }
  34.     this.getOverrideMethod = function() {
  35.         return "override getOverrideMethod";
  36.     }
  37. }
  38. System = {}
  39. System.out = {}
  40. System.out.println = function() {
  41.     if(arguments.length == 1) {
  42.         document.write(arguments[0] + "<br />/n");
  43.     } else {
  44.         document.write("<br />/n");
  45.     }
  46. }
  47. var subClass = new SubSubClass();
  48. System.out.println("1. field = /"" + subClass.field + "/"/tmethod = /"" + subClass.getMethod() + "/"");
  49. System.out.println("2. f = /"" + subClass.f + "/"/tm = /"" + subClass.getM() + "/"");
  50. System.out.println("3. subf = /"" + subClass.subf + "/"/tsubm = /"" + subClass.getSubM() + "/"");
  51. System.out.println("4. override the method = /"" + subClass.getOverrideMethod() + " of BaseClass/"");
  52. </script>
  53. </body>
  54. </html>

here is the result:

 

===================================================

1. field = "field" method = "getMethod"

2. f = "f" m = "getM"

3. subf = "subf" subm = "getSubM"

4. override the method = "override getOverrideMethod of BaseClass"

===================================================