This is a simple test of javascript's inheritance,you also can change the "apply" method to "call". the result is same.
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <title>Javascript Class extend test</title>
- <meta name="Generator" content="EditPlus">
- <meta name="Author" content="^_^-Stefli,^_^-Beluga">
- <meta name="Keywords" content="">
- <meta name="Description" content="">
- </head>
- <body>
- <script>
- function BaseClass() {
- this.field = "field";
- this.getMethod = function() {
- return "getMethod";
- }
- this.getOverrideMethod = function() {
- return "getOverrideMethod";
- }
- }
- function SubClass() {
- BaseClass.call(this);
- this.f = "f";
- this.getM = function() {
- return "getM";
- }
- }
- function SubSubClass() {
- SubClass.call(this);
- this.subf = "subf";
- this.getSubM = function() {
- return "getSubM";
- }
- this.getOverrideMethod = function() {
- return "override getOverrideMethod";
- }
- }
- System = {}
- System.out = {}
- System.out.println = function() {
- if(arguments.length == 1) {
- document.write(arguments[0] + "<br />/n");
- } else {
- document.write("<br />/n");
- }
- }
- var subClass = new SubSubClass();
- System.out.println("1. field = /"" + subClass.field + "/"/tmethod = /"" + subClass.getMethod() + "/"");
- System.out.println("2. f = /"" + subClass.f + "/"/tm = /"" + subClass.getM() + "/"");
- System.out.println("3. subf = /"" + subClass.subf + "/"/tsubm = /"" + subClass.getSubM() + "/"");
- System.out.println("4. override the method = /"" + subClass.getOverrideMethod() + " of BaseClass/"");
- </script>
- </body>
- </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"
===================================================