Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 4th, 2007, 11:06 AM
Authorized User
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kaos_frack
Default inheritance problem

I'm trying to create an object (car1) inside another object (Garage).
car1 object should inherit from Car object and define it's own members. But the code below is not working. Can someone tell me the reason and maybe a solution.
<script type="text/javascript">
function Car(sColor) {
   this.color = sColor;

Car.prototype.sayColor = function() {

var Garage = {
   car1: {
      prototype: new Car("red"),
      owner: "john"

alert(Garage.car1.owner); // outputs "john"
alert(Garage.car1.color); // outputs "undefined"
alert(Garage.car1.prototype.color); // outputs "red"
Garage.car1.prototype.sayColor(); // outputs "red"
Garage.car1.sayColor(); // fails
Why wouldn't this kind of inheritance work?
Isn't it (the statement in red) the same as:
car1.prototype = new Car("red");
EDIT: I even tried this:
var Garage = {
   car1: function() {},
   car1.prototype: new Car("red"),
   car1.owner: "john"
But no use.

Reply With Quote
  #2 (permalink)  
Old March 25th, 2007, 09:28 AM
Authorized User
Join Date: Mar 2006
Location: Munich, , Germany.
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts

var Garage = {
   car1: {
      prototype: new Car("red"),
      owner: "john"
car1 is in this case a property of the Garage object. The propertys value is a literal object value. Only objects created calling a constructor has an associated prototype property.

So what you do in your code is to simply add a property called prototype and assign a new Car object to it. There is no inheritance in your code. Thats why Garage.car1.color is undefined
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem With Inheritance jazzcatone ASP.NET 2.0 Basics 1 April 23rd, 2006 11:33 AM
Inheritance problem flyin General .NET 2 May 20th, 2004 10:55 AM
c# inheritance bhohman C# 2 March 26th, 2004 01:47 PM
Vectors/inheritance problem hpy_gilmore8 C++ Programming 1 February 23rd, 2004 08:21 PM

All times are GMT -4. The time now is 09:57 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.