...

View Full Version : Using a .JS file "Undefined" message returning an objects value



johnnybutler
09-20-2005, 01:56 PM
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;
}

nikkiH
09-20-2005, 03:07 PM
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.

johnnybutler
09-20-2005, 03:32 PM
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.

nikkiH
09-20-2005, 04:49 PM
Circle.js



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;
}

johnnybutler
09-22-2005, 10:18 AM
Thanks NikkiH!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum