michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: File Name: instanceof-002.js michael@0: Section: michael@0: Description: Determining Instance Relationships michael@0: michael@0: This test is the same as js1_3/inherit/proto-002, except that it uses michael@0: the builtin instanceof operator rather than a user-defined function michael@0: called InstanceOf. michael@0: michael@0: This tests Object Hierarchy and Inheritance, as described in the document michael@0: Object Hierarchy and Inheritance in JavaScript, last modified on 12/18/97 michael@0: 15:19:34 on http://devedge.netscape.com/. Current URL: michael@0: http://devedge.netscape.com/docs/manuals/communicator/jsobj/contents.htm michael@0: michael@0: This tests the syntax ObjectName.prototype = new PrototypeObject using the michael@0: Employee example in the document referenced above. michael@0: michael@0: Author: christine@netscape.com michael@0: Date: 12 november 1997 michael@0: */ michael@0: // onerror = err; michael@0: michael@0: var SECTION = "instanceof-002"; michael@0: var VERSION = "ECMA_2"; michael@0: var TITLE = "Determining Instance Relationships"; michael@0: michael@0: startTest(); michael@0: writeHeaderToLog( SECTION + " "+ TITLE); michael@0: michael@0: function InstanceOf( object, constructor ) { michael@0: while ( object != null ) { michael@0: if ( object == constructor.prototype ) { michael@0: return true; michael@0: } michael@0: object = object.__proto__; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: function Employee ( name, dept ) { michael@0: this.name = name || ""; michael@0: this.dept = dept || "general"; michael@0: } michael@0: michael@0: function Manager () { michael@0: this.reports = []; michael@0: } michael@0: Manager.prototype = new Employee(); michael@0: michael@0: function WorkerBee ( name, dept, projs ) { michael@0: this.base = Employee; michael@0: this.base( name, dept) michael@0: this.projects = projs || new Array(); michael@0: } michael@0: WorkerBee.prototype = new Employee(); michael@0: michael@0: function SalesPerson () { michael@0: this.dept = "sales"; michael@0: this.quota = 100; michael@0: } michael@0: SalesPerson.prototype = new WorkerBee(); michael@0: michael@0: function Engineer ( name, projs, machine ) { michael@0: this.base = WorkerBee; michael@0: this.base( name, "engineering", projs ) michael@0: this.machine = machine || ""; michael@0: } michael@0: Engineer.prototype = new WorkerBee(); michael@0: michael@0: var pat = new Engineer(); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat.__proto__ == Engineer.prototype", michael@0: true, michael@0: pat.__proto__ == Engineer.prototype ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat.__proto__.__proto__ == WorkerBee.prototype", michael@0: true, michael@0: pat.__proto__.__proto__ == WorkerBee.prototype ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat.__proto__.__proto__.__proto__ == Employee.prototype", michael@0: true, michael@0: pat.__proto__.__proto__.__proto__ == Employee.prototype ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype", michael@0: true, michael@0: pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null", michael@0: true, michael@0: pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat instanceof Engineer", michael@0: true, michael@0: pat instanceof Engineer ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat instanceof WorkerBee )", michael@0: true, michael@0: pat instanceof WorkerBee ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat instanceof Employee )", michael@0: true, michael@0: pat instanceof Employee ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat instanceof Object )", michael@0: true, michael@0: pat instanceof Object ); michael@0: michael@0: new TestCase( SECTION, michael@0: "pat instanceof SalesPerson )", michael@0: false, michael@0: pat instanceof SalesPerson ); michael@0: test();