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