![]() |
Object Orientated Problem
Hey guy's,
im pretty new to oop JS programming. Here is my problem: Code:
function Ships(){how can i access the right mother object? Thank you! |
Assuming you have two objects -
Code:
var motherShip = new Ships();Code:
motherShip.faster(); // this will set speed = 2Reason: 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 = 2Hope this may help you out... Regards, Niral Soni |
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? |
Code:
function Ships(){Code:
|
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! |
Lots of ways to do this
here is one .... Code:
|
| All times are GMT +1. The time now is 01:28 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.