Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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.
Code:
<script type="text/javascript">
function Car(sColor) {
   this.color = sColor;
}

Car.prototype.sayColor = function() {
   alert(this.color);
};

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
</script>
Why wouldn't this kind of inheritance work?
Isn't it (the statement in red) the same as:
Code:
car1.prototype = new Car("red");
EDIT: I even tried this:
Code:
var Garage = {
   car1: function() {},
   car1.prototype: new Car("red"),
   car1.owner: "john"
};
But no use.

Thanks
  #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
Default

Code:
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


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





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