PDA

View Full Version : Inheritance problem



El Bicho
January 27th, 2004, 12:55 PM
Can you take a look at this code?
Although noFoot has no feet he still has 20 fingers. Whatīs wrong here? Does he have 10 extra fingers coming out of his head?



function Extremities() {
this.hands = 2;
this.feet = 2;
}
Fingers.prototype = new Extremities();
function Fingers() {
this.Fingers = this.hands * 5 + this.feet * 5;
}
twoFeet = new Fingers();
noFoot = new Fingers();
noFoot.feet=0
trace("twoFeet has " + twoFeet.Fingers + " fingers");
trace("noFoot has " + noFoot.feet + " feet");
trace("noFoot has " + noFoot.Fingers + " fingers");

ScriptFlipper
January 27th, 2004, 01:15 PM
is there any difference if you put a semicolon after the line noFoot.feet=0
like this

noFoot.feet = 0;

El Bicho
January 27th, 2004, 01:20 PM
Hi, not really. If you forget a semicolon it can be added with autoformat and you donīt receive any error message. There are plenty of scripts out there running without semicolons.:)

senocular
January 27th, 2004, 01:25 PM
http://www.kirupa.com/developer/oop/AS1OOPInheritance4.htm

El Bicho
February 3rd, 2004, 11:30 AM
Hi, thanks, I followed the link but I am still very confused...:upset:

Just what should I add/change/delete to make noFoot have 10 fingers instead of 20?

Voetsjoeba
February 3rd, 2004, 01:40 PM
You are setting the feet property to zero after having created the instance. That's why it still has 20 fingers, because the 20 fingers were assigned when it was being created.

You can't first delete it's feet and then create it. So what I'd do is create a method that reassigns fingers. You can then have noFeet execute this method after you've deleted his feet.



function Extremities() {
this.hands = 2;
this.feet = 2;
}
function Fingers() {
this.fingers = this.hands*5+this.feet*5;
}
Fingers.prototype = new Extremities();
Fingers.prototype.updateFingers = function(){
this.fingers = this.hands*5+this.feet*5;
}
twoFeet = new Fingers();
noFoot = new Fingers();
noFoot.feet = 0;
noFoot.updateFingers();
trace("twoFeet has "+twoFeet.fingers+" fingers");
trace("noFoot has "+noFoot.feet+" feet");
trace("noFoot has "+noFoot.fingers+" fingers");
//twoFeet has 20 fingers
//noFoot has 0 feet
//noFoot has 10 fingers