2 File Name: instanceof-002.js
4 Description: Determining Instance Relationships
6 This test is the same as js1_3/inherit/proto-002, except that it uses
7 the builtin instanceof operator rather than a user-defined function
10 This tests Object Hierarchy and Inheritance, as described in the document
11 Object Hierarchy and Inheritance in JavaScript, last modified on 12/18/97
12 15:19:34 on http://devedge.netscape.com/. Current URL:
13 http://devedge.netscape.com/docs/manuals/communicator/jsobj/contents.htm
15 This tests the syntax ObjectName.prototype = new PrototypeObject using the
16 Employee example in the document referenced above.
18 Author: christine@netscape.com
19 Date: 12 november 1997
23 var SECTION = "instanceof-002";
24 var VERSION = "ECMA_2";
25 var TITLE = "Determining Instance Relationships";
28 writeHeaderToLog( SECTION + " "+ TITLE);
31 var testcases = new Array();
33 function InstanceOf( object, constructor ) {
34 while ( object != null ) {
35 if ( object == constructor.prototype ) {
38 object = object.__proto__;
43 function Employee ( name, dept ) {
44 this.name = name || "";
45 this.dept = dept || "general";
51 Manager.prototype = new Employee();
53 function WorkerBee ( name, dept, projs ) {
55 this.base( name, dept)
56 this.projects = projs || new Array();
58 WorkerBee.prototype = new Employee();
60 function SalesPerson () {
64 SalesPerson.prototype = new WorkerBee();
66 function Engineer ( name, projs, machine ) {
67 this.base = WorkerBee;
68 this.base( name, "engineering", projs )
69 this.machine = machine || "";
71 Engineer.prototype = new WorkerBee();
73 var pat = new Engineer()
75 testcases[tc++] = new TestCase( SECTION,
76 "pat.__proto__ == Engineer.prototype",
78 pat.__proto__ == Engineer.prototype );
80 testcases[tc++] = new TestCase( SECTION,
81 "pat.__proto__.__proto__ == WorkerBee.prototype",
83 pat.__proto__.__proto__ == WorkerBee.prototype );
85 testcases[tc++] = new TestCase( SECTION,
86 "pat.__proto__.__proto__.__proto__ == Employee.prototype",
88 pat.__proto__.__proto__.__proto__ == Employee.prototype );
90 testcases[tc++] = new TestCase( SECTION,
91 "pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype",
93 pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype );
95 testcases[tc++] = new TestCase( SECTION,
96 "pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null",
98 pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null );
100 testcases[tc++] = new TestCase( SECTION,
101 "pat instanceof Engineer",
103 pat instanceof Engineer );
105 testcases[tc++] = new TestCase( SECTION,
106 "pat instanceof WorkerBee )",
108 pat instanceof WorkerBee );
110 testcases[tc++] = new TestCase( SECTION,
111 "pat instanceof Employee )",
113 pat instanceof Employee );
115 testcases[tc++] = new TestCase( SECTION,
116 "pat instanceof Object )",
118 pat instanceof Object );
120 testcases[tc++] = new TestCase( SECTION,
121 "pat instanceof SalesPerson )",
123 pat instanceof SalesPerson );