最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript showing the function text instead of printing value on screen - Stack Overflow

programmeradmin3浏览0评论
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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

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.

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>
发布评论

评论列表(0)

  1. 暂无评论