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