CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Object Orientated Problem (http://www.codingforums.com/showthread.php?t=285147)

explofish 01-02-2013 12:53 PM

Object Orientated Problem
 
Hey guy's,

im pretty new to oop JS programming. Here is my problem:

Code:

function Ships(){
this.speed=1
}

Ships.prototype.faster=function{
this.speed=this.speed+1
}

function Ship(color){ //Child of Ships
 
  Ships.call(this);// Call the parent constructor

        this.color=color; //Color for the Ship. Used in JS Code
}

Ship.prototype = new Ships();// inherit Ships

Ship.prototype.constructor=Ship;// correct the constructor pointer

ship.prototype.animate = function(){
alert(this.speed)
}

As you see, ship should be a child of ships. Now the problem is that if i call ships.faster() the child function ship.animate() accesses an old value of this.speed.

how can i access the right mother object?

Thank you!

niralsoni 01-02-2013 01:20 PM

Assuming you have two objects -
Code:

var motherShip = new Ships();
var childShip = new Ship();

And then you are trying to call the faster() function of parent object, and want that value to be reflected in your child object... something like below -
Code:

motherShip.faster(); // this will set speed = 2
childShip.animate(); // this is still referring to speed = 1

If above assumptions are correct, and that is how you are calling the functions, then I am sure it wont achieve the expected result.

Reason:
When you create two objects, two different memory space is created for all variables and methods defined in those objects. So, in this case, two different variables "speed" are created for two different objects - motherShip and childShip.

If you want to access the parent variable in the child object, you need to call the parent method using child object only.

So, the code would be something like shown below -
Code:

childShip.faster(); // this will set speed = 2
childShip.animate(); // this will  now refer to speed = 2

Can you please provide how you are defining the objects and calling the functions?

Hope this may help you out...

Regards,
Niral Soni

explofish 01-02-2013 03:44 PM

okay thank you! for the good explanation :)

but if i have multiple child instances, how can i access the mother vars and methods in the same context?

DaveyErwin 01-02-2013 03:48 PM

Code:

function Ships(){
this.speed=1
}

Ships.prototype.faster=function{
this.speed=this.speed+1
}

function Ship(color){ //Child of Ships
 
  Ships.call(this);// Call the parent constructor

this.color=color; //Color for the Ship. Used in JS Code
}

Ship.prototype = new Ships();// inherit Ships

Ship.prototype.constructor=Ship;// correct the constructor pointer

ship.prototype.animate = function(){// should be Ship not ship
alert(this.speed)
}







Code:


<script>
function Ships(){
    this.speed=1
}
Ships.prototype.faster=function(){
    this.speed=this.speed+1
}
Ship.prototype = new Ships();// inherit Ships
Ship.prototype.constructor=Ship;// correct the constructor pointer
function Ship(color){ //Child of Ships 
    Ships.call(this);// Call the parent constructor
    this.color=color; //Color for the Ship. Used in JS Code
}
Ship.prototype.animate = function(){
    return this.speed;
}
 
a= new Ship;
b= new Ship;
alert("ship b speed = "+b.animate());
b.faster();
alert("ship b speed = "+b.animate());
alert("ship a speed = "+a.animate());
a.faster();
alert("ship a speed = "+a.animate());
</script>


explofish 01-04-2013 08:01 PM

okay thank you so far. the example was maype a bit confusing.

just imagine speed of ships as speed of the replay (time multiplicator). so i want to have the mother class ships to be with the same values for all ships. so if in your example
alert1 should be 1
alert 2 should be 2
alert 3 should be 2
alert 4 should be 3

the attribute color should be seperate for each ship. thats why its in the child class.

thank you!

DaveyErwin 01-04-2013 09:31 PM

Lots of ways to do this
here is one ....

Code:


<script>
Ship = function (){
    var speed = 0;
    return function (color){
 this.faster = function(){speed+=1}
 this.color=color;
 this.animate = function(){
    return speed;}
    }
}();
a= new Ship("red");
b= new Ship("blue");
alert(b.color + " ship speed = "+b.animate());
b.faster();
alert(b.color + " ship speed = "+b.animate());
alert(a.color + " ship speed = "+a.animate());
a.faster();
alert(a.color + " ship speed = "+a.animate());
</script>



All times are GMT +1. The time now is 01:28 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.