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