newbie prototype problem.

M

Morgan

Hello,

I'm trying to learn about prototypes in JavaScript and have run into
this problem I can't make head nor tail of.

I'm trying to give the Robot prototype object to the UltraRobot
prototype so that I could access the properties of the Robot prototype
object through an instance of the UltraRobot object.
Which works fine. However now I can't access the UltraRobot prototype
for some reason, I don't know.

Here below is two constructors, Robot and UltraRobot, they each have a
prototype that should give them two different types of alert messages
depending on which I access. The crux of the problem is that the
debugger tells me that guard.hello2 is not a function. Why is this?
Please help, I've been staring at the code for some time now. :(

Robot.prototype.hello1 = function()
{
alert("robot");
}

UltraRobot.prototype.hello2 = function()
{
alert("ultrarobot");
}

//accessing the properties of the constructors isn't a problem.

function Robot()
{
this.propofrobot="property of robot";
}

function UltraRobot()
{
this.propofultrarobot="property of the ultrarobot!";
}

/*
I assigned the Robot prototype to UltraRobot here, hoping to give it
its
prototype, which seems to have worked, but now accessing UltraRobot
prototype
of hello2 isn't happening.
*/

UltraRobot.prototype = new Robot();

var guard = new UltraRobot()

document.write(guard.propofrobot + "<br />");
document.write(guard.propofultrarobot);

guard.hello1()
//And heres the troublesome property:
guard.hello2()


Thanks in advance for any help :)
 
M

Martin Honnen

Morgan wrote:

Robot.prototype.hello1 = function()
{
alert("robot");
}

UltraRobot.prototype.hello2 = function()
{
alert("ultrarobot");
}

//accessing the properties of the constructors isn't a problem.

function Robot()
{
this.propofrobot="property of robot";
}
UltraRobot.prototype = new Robot();

The order of such assignments matters, you need the following order

Robot.prototype.hello1 = function()
{
alert("robot");
}

UltraRobot.prototype = new Robot();

UltraRobot.prototype.hello2 = function()
{
alert("ultrarobot");
}
 
M

Michael Winter

On 25/09/2005 17:54, Morgan wrote:

[snip]
UltraRobot.prototype.hello2 = function()
{
alert("ultrarobot");
}

At this point, the prototype property is an Object object, created for
you when the function declarations are parsed to create function objects.

[snip]
UltraRobot.prototype = new Robot();

But later, you replace this prototype object with one of your own,
taking your newly added function with it.

Assign the new prototype object first, /then/ add any properties to it.

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top