Debian Codegraph @

rhino/testsrc/tests/ecma_2/extensions/instanceof-002.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'instanceof-002.js';
7
8 /**
9 File Name: instanceof-002.js
10 Section:
11 Description: Determining Instance Relationships
12
13 This test is the same as js1_3/inherit/proto-002, except that it uses
14 the builtin instanceof operator rather than a user-defined function
15 called InstanceOf.
16
17 This tests Object Hierarchy and Inheritance, as described in the document
18 Object Hierarchy and Inheritance in JavaScript, last modified on 12/18/97
19 15:19:34 on http://devedge.netscape.com/. Current URL:
20 http://devedge.netscape.com/docs/manuals/communicator/jsobj/contents.htm
21
22 This tests the syntax ObjectName.prototype = new PrototypeObject using the
23 Employee example in the document referenced above.
24
25 Author: [email protected]
26 Date: 12 november 1997
27 */
28 // onerror = err;
29
30 var SECTION = "instanceof-002";
31 var VERSION = "ECMA_2";
32 var TITLE = "Determining Instance Relationships";
33
34 startTest();
35 writeHeaderToLog( SECTION + " "+ TITLE);
36
37 function InstanceOf( object, constructor ) {
38 while ( object != null ) {
39 if ( object == constructor.prototype ) {
40 return true;
41 }
42 object = object.__proto__;
43 }
44 return false;
45 }
46
47 function Employee ( name, dept ) {
48 this.name = name || "";
49 this.dept = dept || "general";
50 }
51
52 function Manager () {
53 this.reports = [];
54 }
55 Manager.prototype = new Employee();
56
57 function WorkerBee ( name, dept, projs ) {
58 this.base = Employee;
59 this.base( name, dept)
60 this.projects = projs || new Array();
61 }
62 WorkerBee.prototype = new Employee();
63
64 function SalesPerson () {
65 this.dept = "sales";
66 this.quota = 100;
67 }
68 SalesPerson.prototype = new WorkerBee();
69
70 function Engineer ( name, projs, machine ) {
71 this.base = WorkerBee;
72 this.base( name, "engineering", projs )
73 this.machine = machine || "";
74 }
75 Engineer.prototype = new WorkerBee();
76
77 var pat = new Engineer();
78
79 new TestCase( SECTION,
80 "pat.__proto__ == Engineer.prototype",
81 true,
82 pat.__proto__ == Engineer.prototype );
83
84 new TestCase( SECTION,
85 "pat.__proto__.__proto__ == WorkerBee.prototype",
86 true,
87 pat.__proto__.__proto__ == WorkerBee.prototype );
88
89 new TestCase( SECTION,
90 "pat.__proto__.__proto__.__proto__ == Employee.prototype",
91 true,
92 pat.__proto__.__proto__.__proto__ == Employee.prototype );
93
94 new TestCase( SECTION,
95 "pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype",
96 true,
97 pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype );
98
99 new TestCase( SECTION,
100 "pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null",
101 true,
102 pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null );
103
104 new TestCase( SECTION,
105 "pat instanceof Engineer",
106 true,
107 pat instanceof Engineer );
108
109 new TestCase( SECTION,
110 "pat instanceof WorkerBee )",
111 true,
112 pat instanceof WorkerBee );
113
114 new TestCase( SECTION,
115 "pat instanceof Employee )",
116 true,
117 pat instanceof Employee );
118
119 new TestCase( SECTION,
120 "pat instanceof Object )",
121 true,
122 pat instanceof Object );
123
124 new TestCase( SECTION,
125 "pat instanceof SalesPerson )",
126 false,
127 pat instanceof SalesPerson );
128 test();