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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Location
    Sunbury, Ohio
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Unexpected Code Problems:

    I have been reading through a book on javascript and I am trying to use a code I found in it.

    This is what I have in the head.

    Code:
    <script language="JavaScript">
    <!--
    function get_price()
    {
    var the_price=1000;
      if (this.speed == '500 MHz')
        the_price+=100;
      else
        the_price+=200;
      if (this.hdspace == '15 GB')
        the_price+=50;
      else
        the_price+=25;
      if (this.ram == '128 MB')
        the_price+=150;
      else
        the_price+=75;
      return the_price;
    }
    
    function computer(speed,hdspace,ram)
    {
      this.speed=speed;
      this.hdspace=hdspace;
      this.ram=ram;
      this.price=get_price;
    }
    
    var work_computer= new computer('2 GHz' , '80 GB' , 1 GB');
    var home_computer= new computer('1.5 GHz' , '40 GB' , '512 MB');
    var laptop_computer= new computer('1 GHz' , '20 GB' , '256 MB');
    
    var work_computer_price= work_computer.price();
    var home_computer_price= home_computer.price();
    var laptop_computer_price= laptop_computer.price();
    //-->
    </script>
    In the body I have a bunch of document.write with the variables located inside. The book was written in 2004 so is it a problem with the edition of the Javascript code or just a mistake I have made?
    Last edited by lgm432; 10-05-2008 at 08:01 PM. Reason: Forgot the code formatting.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by lgm432 View Post
    Code:
    function computer(speed,hdspace,ram)
    {
      this.speed=speed;
      this.hdspace=hdspace;
      this.ram=ram;
      this.price=get_price;
    }
    That should be a function call, not a function address:this.price=get_price();

    Code:
    var work_computer_price= work_computer.price();
    var home_computer_price= home_computer.price();
    var laptop_computer_price= laptop_computer.price();
    These are calls to a member function of computer but get_price isn't a member of computer, so the listing is inconsistent.

    Declare get_price like this:
    Code:
    computer.prototype.get_price=function()
    and in the constructor call it thus:
    Code:
    this.price=this.get_price();
    Last edited by Arty Effem; 10-05-2008 at 08:23 PM.

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Location
    Sunbury, Ohio
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you!
    Last edited by lgm432; 10-05-2008 at 08:31 PM. Reason: Relooked over code


  •  

    Tags for this Thread

    Posting Permissions

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