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