2009-12-05 32 views
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); 

當加載的index.htm,結果反而在屏幕上寫的返回值的函數:的Javascript顯示功能文本,而不是打印值在屏幕上

功能乘法(){返回this.x * this.y; }

:(

回答

6

你忘了():

document.write(hexagon.multiply()); 

如果你不使用(),使用Javascript將把multiply作爲一個變量,寫出它的內容 - 在這種情況下, 。該函數的代碼

+0

哈!太簡單。丹科! – 2009-12-05 20:55:20

+0

De nada! (從姓氏中猜測語言環境 - 如果我錯了,請原諒我) – 2009-12-05 21:07:03

2

你必須確保你的JavaScript代碼是<script></script>標籤因此,它可能是:

<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> 
+2

+1對於我從來不會考慮的解釋。 – 2009-12-05 20:50:16

+1

好的想法,但我認爲這不是重點 - 如果是這樣,它會顯示整個功能,而不僅僅是multiply()。 – 2009-12-05 20:54:35

相關問題