tags:

views:

66

answers:

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

:(

+5  A: 

You 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.

Pekka
Hah! So easy. Danke!
Gabriel A. Zorrilla
De nada! (Guessing locale from last name - forgive me if I'm wrong ;)
Pekka
+1  A: 

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>
alexy13
+1 for an interpretation I never would have considered.
Adam A
Good thought but not the point here I think - if it were, it would show the whole function and not only multiply().
Pekka