[Javascript] Question about best method of creating inheritance in javascript

Dan Costea costea.dan at ssi-schaefer.ro
Wed Apr 24 20:43:06 CDT 2002


Amanda, when I red this book (Client-Side JavaScript Guide) I made few notes
about using objects. I hope these notes can help you. Here they are:


/***************************************************************************
********/
 Important observations:
 - JavaScript is not a class-based language (like JAVA or C++). It is
prototype-based language. A prototype-based language has the notion of a
prototypical object, an object used as a template from which to get the
initial properties for a new object.
 - JavaScript does not have a class definition separate from the
constructor.
 - Any JavaScript function can be used as a constructor.

OBJECTS:


// this is a constructor for the "person" object
function person (name, age, sex)
{
 // a js object constructor is a function that have the object name and its
properties, declared as follwos:
 this.name = name;
 this.age = age;
 this.sex = sex;
}

// these are few instances of the "person" object
Dan = new person ("Dan Costea", 23, "M");
Ion = new person ("Ion Iliescu", 98, "F");
Adelina = new person ("Simona", 21, "F");

// making another object:
function car (make, model, color, owner)
{
 this.make = make;
 this.model = model;
 this.color = color;
 this.owner = owner;
 // defineing a method: (display_car is a function defined bellow)
 this.fnDisplay_car = fnDisplay_car;
}

// these are few instances of the "car" object
ford = new car ("Ford", "Mondeo", "red", Adelina);
mercedes = new car ("Mercedes", "A Classe", "black", Dan);
dacia = new car ("Dacia", "Nova", "blue", Ion);
// for accesing the mercedes owner age, you have to say: mercedes.owner.age

// changeing a property:
mercedes.model = "limusine";

// adding a property just for a specific object:
mercedes.year = 2001;

// adding a property for all "car" object types:
car.prototype.country = null;

// modifying this new property for mercedes object:
mercedes.country = "Gremany";


// a method of the car object (it is written exactly like a normal function,
 but it is declared in the object constructor)
function fnDisplay_car ()
{
 alert ("This is a method of the car object." + "\n" + "You selected " +
this.make + " car." + "\n" + this.owner.name + " is the owner of this
car.");
}


// function that displays object properties (defined or predefined objects)
function fnShow_props (obj, obj_name)
{
 var result = "";
 // going through each object property:
 for (var i in obj)
  result += obj_name + "." + i + " = " + obj[i] + "\n"
 return result
}




INHERITANCE (1):


// the top level object type
function Employee ()
{
 this.name = "";
 this.dept = "general";
}


// object that inheritance Employee object and adds one new property
function Manager ()
{
 this.reports = [];
}
Manager.prototype = new Employee;


// object that inheritance Employee object and adds one new property
function WorkerBee ()
{
 this.projects = [];
}
WorkerBee.prototype = new Employee;


// object that inheritance WorkerBee object, adds one new property (quota)
and modifies the value of one already defined property (dept)
function SalesPerson ()
{
 this.dept = "sales";
 this.quota = 100;
}
SalesPerson.prototype = new WorkerBee;


// object that inheritance WorkerBee object, adds one new property (machine)
and modifies the value of one already defined property (dept)
function Engineer ()
{
 this.dept = "engineering";
 this.machine = "";
}
Engineer.prototype = new WorkerBee;



// creating few objects that have WorkerBee as model (we can say that these
objects are "instances" of WorkerBee)
jim = new Employee;
sally = new Manager;
mark = new WorkerBee;
fred = new SalesPerson;
jane = new Engineer;
// I think it's obvious what properties each object will have (and how can
you change them or add new properties - see creating object example)




INHERITANCE (2):


// the top level object type
function Employee ()
{
 this.name = "";
 this.dept = "general";
}


// object that inheritance Employee object and adds one new property
function Manager ()
{
 this.reports = [];
}
Manager.prototype = new Employee;


// object that inheritance Employee object and adds one new property
function WorkerBee ()
{
 this.projects = [];
}
WorkerBee.prototype = new Employee;


// object that inheritance WorkerBee object, adds one new property (quota)
and modifies the value of one already defined property (dept)
function SalesPerson ()
{
 this.dept = "sales";
 this.quota = 100;
}
SalesPerson.prototype = new WorkerBee;


// object that inheritance WorkerBee object, adds one new property (machine)
and modifies the value of one already defined property (dept)
function Engineer ()
{
 this.dept = "engineering";
 this.machine = "";
}
Engineer.prototype = new WorkerBee;



// creating few objects that have WorkerBee as model (we can say that these
objects are "instances" of WorkerBee)
jim = new Employee;
sally = new Manager;
mark = new WorkerBee;
fred = new SalesPerson;
jane = new Engineer;
// I think it's obvious what properties each object will have (and how can
you change them or add new properties - see creating object example)



// function that displays object properties (defined or predefined objects)
function fnShow_props (obj, obj_name)
{
 var result = "";
 // going through each object property:
 for (var i in obj)
  result += obj_name + "." + i + " = " + obj[i] + "\n"
 return result
}

/***************************************************************************
********/




----- Original Message -----
From: "Amanda Birmingham" <lists at imladris.com>
To: <javascript at LaTech.edu>
Sent: Wednesday, April 24, 2002 8:11 AM
Subject: Re: [Javascript] Question about best method of creating inheritance
in javascript


> Dan,
> Thank you for the recommendation; could you be more specific about what
you
> feel would be helpful in this chapter?  I have read Flanagan's Definitive
> Guide, and didn't immediately see any extra information in the recommended
> reading.
>
> Perhaps I should have noted that I am familiar with javascript, and have
> used objects in it before ... my current mission is to try to track down
> info about the more esoteric aspects of inheritance so that I do it in a
> performance-friendly way (my new app will have a very large number of
> heavily-subclassed objects, and I don't want my chosen inheritance
> mechanism to create any more than are strictly necessary.)
>
> Thanks for your time,
> Amanda Birmingham
> Web Application Developer
>
> At 10:18 AM 4/24/2002 -0700, you wrote:
>
> >I recommend you to read the chapter "Working with Objects" of
"Client-Side
> >JavaScript Guide" book:
> >http://developer.netscape.com/docs/manuals/js/client/jsguide/obj.htm
> >
> >Dan.




More information about the Javascript mailing list