Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Hierarchy and Inheritance in JavaScript 2

Status
Not open for further replies.
I stumbled across this link and thought some of you might find it interesting. It talks about how to make an object inherit from another object. For example, I can make a "Developer" object inherit all the properties and methods of the more generic "Person" object.
Code:
<script>
Person = function(name){
  this.name = name || "";
  this.birthDate = "unknown";
}

Developer = function(name, skills){
  this.base = Person;
  this.base(name);
  this.skills = skills || [];

}
Developer.prototype = new Person;

var newGuy = new Developer("Bob", ["JavaScript","ASP"]);
alert(newGuy.name)
alert(newGuy.birthDate)
alert(newGuy.skills)

</script>
So if I went and added a property to the "Person" object (at either design-time or run-time), any objects inheriting from that object would also get it. This may cut down on the amount of code and maintenance time for your project if your project is complex enough to warrant using such a technique.

Adam

There are only 10 types of people in the world: Those who understand binary, and those who don't
 
My projects are frequently complex enough to warrant that kind of thing. Thanks for the tip.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 

Thanks Adam...

I'd read that entire document before, but had forgotten one of the crucial bits of information it mentioned - which I just found myself needing. Handy, eh?!

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top