function hex(x,y,side,isLast,color) {//Hex object constructor. this.x = x; this.y = y; this.side = side; this.isLast = isLast; this.color = color; function multiply() { return this.x * this.y; } this.multiply = multiply; } var hexagon = new hex(22,22,20,0,1); document.write(hexagon.multiply);
When loading index.htm, results that writes on screen the function instead of the returning value:
function multiply() { return this.x * this.y; }
:(
function hex(x,y,side,isLast,color) {//Hex object constructor. this.x = x; this.y = y; this.side = side; this.isLast = isLast; this.color = color; function multiply() { return this.x * this.y; } this.multiply = multiply; } var hexagon = new hex(22,22,20,0,1); document.write(hexagon.multiply);
When loading index.htm, results that writes on screen the function instead of the returning value:
function multiply() { return this.x * this.y; }
:(
Share Improve this question asked Dec 5, 2009 at 20:42 GabrielGabriel 5,75418 gold badges66 silver badges97 bronze badges2 Answers
Reset to default 7You forgot the ():
document.write(hexagon.multiply());
If you don't use (), Javascript will treat multiply
as a variable and write out it contents - in this case, the code of the function.
You have to make sure that your javascript code is in <script>
and </script>
tags. So, it might read:
<html><head><script type="text/javascript">
function hex(x,y,side,isLast,color)
{//Hex object constructor.
this.x = x;
this.y = y;
this.side = side;
this.isLast = isLast;
this.color = color;
function multiply()
{
return this.x * this.y;
}
this.multiply = multiply;
}
var hexagon = new hex(22,22,20,0,1);
document.write(hexagon.multiply)
</script>
<body>
<!--Content here-->
</body>
</html>