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_8.js
     24     Section:
     25     Description:        Adding Properties to the Prototype Object
     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     Author:             christine (at) netscape.com
     36     Date:               12 november 1997
     37 */
     38 
     39     var SECTION = "proto_8";
     40     var VERSION = "JS1_3";
     41     var TITLE   = "Adding Properties to the Prototype Object";
     42 
     43     startTest();
     44     writeHeaderToLog( SECTION + " "+ TITLE);
     45 
     46     var testcases = new Array();
     47 
     48 function Employee ( name, dept ) {
     49      this.name = name || "";
     50      this.dept = dept || "general";
     51 }
     52 function WorkerBee ( name, dept, projs ) {
     53     this.base = Employee;
     54     this.base( name, dept)
     55     this.projects = projs || new Array();
     56 }
     57 WorkerBee.prototype = new Employee();
     58 
     59 function Engineer ( name, projs, machine ) {
     60     this.base = WorkerBee;
     61     this.base( name, "engineering", projs )
     62     this.machine = machine || "";
     63 }
     64 Engineer.prototype = new WorkerBee();
     65 
     66 function test() {
     67     for ( tc=0; tc < testcases.length; tc++ ) {
     68         testcases[tc].passed = writeTestCaseResult(
     69                             testcases[tc].expect,
     70                             testcases[tc].actual,
     71                             testcases[tc].description +" = "+
     72                             testcases[tc].actual );
     73 
     74         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     75     }
     76     stopTest();
     77     return ( testcases );
     78 }
     79     var pat = new Engineer( "Toonces, Pat",
     80                             ["SpiderMonkey", "Rhino"],
     81                             "indy" );
     82 
     83     Employee.prototype.specialty = "none";
     84 
     85 
     86     // Pat, the Engineer
     87 
     88     testcases[tc++] = new TestCase( SECTION,
     89                                     "pat.name",
     90                                     "Toonces, Pat",
     91                                     pat.name );
     92 
     93     testcases[tc++] = new TestCase( SECTION,
     94                                     "pat.dept",
     95                                     "engineering",
     96                                     pat.dept );
     97 
     98     testcases[tc++] = new TestCase( SECTION,
     99                                     "pat.projects.length",
    100                                     2,
    101                                     pat.projects.length );
    102 
    103     testcases[tc++] = new TestCase( SECTION,
    104                                     "pat.projects[0]",
    105                                     "SpiderMonkey",
    106                                     pat.projects[0] );
    107 
    108     testcases[tc++] = new TestCase( SECTION,
    109                                     "pat.projects[1]",
    110                                     "Rhino",
    111                                     pat.projects[1] );
    112 
    113     testcases[tc++] = new TestCase( SECTION,
    114                                     "pat.machine",
    115                                     "indy",
    116                                     pat.machine );
    117 
    118     testcases[tc++] = new TestCase( SECTION,
    119                                     "pat.specialty",
    120                                     "none",
    121                                     pat.specialty );
    122 
    123     test();
    124