Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-02-2013, 12:53 PM   PM User | #1
explofish
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
explofish is an unknown quantity at this point
Post 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!
explofish is offline   Reply With Quote
Old 01-02-2013, 01:20 PM   PM User | #2
niralsoni
Regular Coder

 
Join Date: Mar 2008
Location: London
Posts: 129
Thanks: 1
Thanked 31 Times in 31 Posts
niralsoni is an unknown quantity at this point
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
niralsoni is offline   Reply With Quote
Users who have thanked niralsoni for this post:
explofish (01-02-2013)
Old 01-02-2013, 03:44 PM   PM User | #3
explofish
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
explofish is an unknown quantity at this point
Post

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?
explofish is offline   Reply With Quote
Old 01-02-2013, 03:48 PM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 809
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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>

Last edited by DaveyErwin; 01-02-2013 at 03:51 PM..
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
explofish (01-04-2013)
Old 01-04-2013, 08:01 PM   PM User | #5
explofish
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
explofish is an unknown quantity at this point
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!
explofish is offline   Reply With Quote
Old 01-04-2013, 09:31 PM   PM User | #6
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 809
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
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>
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:19 PM.


Advertisement
Log in to turn off these ads.