Home | History | Annotate | Download | only in inherit
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          proto_4.js
     24     Section:
     25     Description:        new PrototypeObject
     26 
     27     This tests Object Hierarchy and Inheritance, as described in the document
     28     Object Hierarchy and Inheritance in JavaScript, last modified on 12/18/97
     29     15:19:34 on http://devedge.netscape.com/.  Current URL:
     30     http://devedge.netscape.com/docs/manuals/communicator/jsobj/contents.htm
     31 
     32     This tests the syntax ObjectName.prototype = new PrototypeObject using the
     33     Employee example in the document referenced above.
     34 
     35     If you add a property to an object in the prototype chain, instances of
     36     objects that derive from that prototype should inherit that property, even
     37     if they were instatiated after the property was added to the prototype object.
     38 
     39 
     40     Author:             christine (at) netscape.com
     41     Date:               12 november 1997
     42 */
     43 
     44     var SECTION = "proto_3";
     45     var VERSION = "JS1_3";
     46     var TITLE   = "Adding properties to the prototype";
     47 
     48     startTest();
     49     writeHeaderToLog( SECTION + " "+ TITLE);
     50 
     51     var testcases = new Array();
     52 
     53 function Employee () {
     54      this.name = "";
     55      this.dept = "general";
     56 }
     57 function Manager () {
     58      this.reports = [];
     59 }
     60 Manager.prototype = new Employee();
     61 
     62 function WorkerBee () {
     63      this.projects = new Array();
     64 }
     65 
     66 WorkerBee.prototype = new Employee();
     67 
     68 function SalesPerson () {
     69     this.dept = "sales";
     70     this.quota = 100;
     71 }
     72 SalesPerson.prototype = new WorkerBee();
     73 
     74 function Engineer () {
     75     this.dept = "engineering";
     76     this.machine = "";
     77 }
     78 Engineer.prototype = new WorkerBee();
     79 
     80 function test() {
     81     for ( tc=0; tc < testcases.length; tc++ ) {
     82         testcases[tc].passed = writeTestCaseResult(
     83                             testcases[tc].expect,
     84                             testcases[tc].actual,
     85                             testcases[tc].description +" = "+
     86                             testcases[tc].actual );
     87 
     88         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     89     }
     90     stopTest();
     91     return ( testcases );
     92 }
     93 
     94     var jim = new Employee();
     95     var terry = new Engineer();
     96     var sean = new SalesPerson();
     97     var wally = new Manager();
     98 
     99     Employee.prototype.specialty = "none";
    100 
    101     var pat = new Employee();
    102     var leslie = new Engineer();
    103     var bubbles = new SalesPerson();
    104     var furry = new Manager();
    105 
    106     Engineer.prototype.specialty = "code";
    107 
    108     var chris = new Engineer();
    109 
    110 
    111     testcases[tc++] = new TestCase( SECTION,
    112                                     "jim = new Employee(); jim.specialty",
    113                                     "none",
    114                                     jim.specialty );
    115 
    116     testcases[tc++] = new TestCase( SECTION,
    117                                     "terry = new Engineer(); terry.specialty",
    118                                     "code",
    119                                     terry.specialty );
    120 
    121     testcases[tc++] = new TestCase( SECTION,
    122                                     "sean = new SalesPerson(); sean.specialty",
    123                                     "none",
    124                                     sean.specialty );
    125 
    126     testcases[tc++] = new TestCase( SECTION,
    127                                     "wally = new Manager(); wally.specialty",
    128                                     "none",
    129                                     wally.specialty );
    130 
    131     testcases[tc++] = new TestCase( SECTION,
    132                                     "furry = new Manager(); furry.specialty",
    133                                     "none",
    134                                     furry.specialty );
    135 
    136     testcases[tc++] = new TestCase( SECTION,
    137                                     "pat = new Employee(); pat.specialty",
    138                                     "none",
    139                                     pat.specialty );
    140 
    141     testcases[tc++] = new TestCase( SECTION,
    142                                     "leslie = new Engineer(); leslie.specialty",
    143                                     "code",
    144                                     leslie.specialty );
    145 
    146     testcases[tc++] = new TestCase( SECTION,
    147                                     "bubbles = new SalesPerson(); bubbles.specialty",
    148                                     "none",
    149                                     bubbles.specialty );
    150 
    151 
    152     testcases[tc++] = new TestCase( SECTION,
    153                                     "chris = new Employee(); chris.specialty",
    154                                     "code",
    155                                     chris.specialty );
    156     test();
    157