Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    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

  • Users who have thanked niralsoni for this post:

    explofish (01-02-2013)

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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?

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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 04:51 PM.

  • Users who have thanked DaveyErwin for this post:

    explofish (01-04-2013)

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!

  • #6
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •