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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using a .JS file "Undefined" message returning an objects value

    I have manged to create a circle object but when i call the getArea() function i get an alert box with "Undefined" as the error message. I tried to pass back a number to test but i got the same "Undefined Message". Any Ideas?

    Exercise8.html
    <head>
    <script type="text/javascript" src="Circle.js"></script>
    <script type="text/javascript">
    function disp_alert(){
    bigCircle = new Circle(100);
    alert(bigCircle.getArea);

    }
    </script>
    </head>

    <body>
    <form>
    <input type="button" onclick="disp_alert()" value="Display alert box">
    </form>
    </body>

    </html>

    Circle.js
    function Circle(radius){
    this.radius = radius;
    }

    function getArea(){
    return this.radius;
    }

    function getCircumference(){
    var diameter = this.radius*2;
    var circumference = diameter*3.14;
    return circumference;
    }

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Function calls in JS require the parentheses.

    bigCircle.getArea();

    To elaborate, if you omit the parens, it thinks you want a property called getArea. Since there isn't one, it's undefined.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have changed the code see below on your advice and still get the same error message. How do i store the radius of the circle? Am i doing it correctly? How do i return that attribute like i am trying below?

    I Appreciate your help, i have looked for examples and tried everything i can think off but nothing has worked.

    Circle.js

    function Circle(radius){
    this.radius = radius;
    this.getArea = Circle.getArea;
    }


    function getradius(){
    return this.radius;
    }

    function getCircumference(){
    var diameter = this.radius*2;
    var circumference = diameter*3.14;
    return circumference;
    }

    Exercise8.html
    <html>
    <head>
    <script type="text/javascript" src="Circle.js"></script>
    <script type="text/javascript">

    function disp_alert(){

    bigCircle = new Circle(100);

    alert(bigCircle.getradius);


    }
    </script>
    </head>

    <body>
    <form>
    <input type="button" onclick="disp_alert()" value="Display alert box">
    </form>
    </body>

    </html>

    I Have also tried

    function getradius(){
    return 2;
    }

    And inside the head using
    alert(bigCircle.getradius());
    for this i am getting an "error on page" error.

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Circle.js

    Code:
    function Circle(radius){
       this.radius = radius;
       function getArea() 
          {
          return ((this.radius*this.radius)*3.14) 
          }
       this.getArea = getArea;
    
       function getRadius()
          {
          return this.radius;
          }
       this.getRadius = getRadius;
    
       function getCircumference()
          {
          var diameter = this.radius*2;
          var circumference = diameter*3.14;
          return circumference;
          }
       this.getCircumference = getCircumference;
    }

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thanks NikkiH!


  •  

    Posting Permissions

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