2015-04-22 65 views
-3

我想打印一個JavaScript函數的結果,而是我得到的整體功能輸出的Javascript錯誤調用函數

輸出

x7: function 
function() { return "hello this is form function"; } 

我想要的輸出:hello this is form function

HTML

<p id="demo"></p> 

的JavaScript

var x7 = function(){ 
    return "hello this is form function"; 
}; 
document.getElementById("demo").innerHTML = "x7: " + typeof x7 + "<br>"+x7+"<br>"; 
+2

再次運行。 – 2015-04-22 16:22:23

+0

@torazaburo,它在下一章現在我得到它 –

+0

沒有()函數,它會返回函數定義。 –

回答

1

你必須使用括號調用函數:x7():通過您的基本JS教程

<!DOCTYPE html> 
<html> 
<body> 

<p id="demo"></p> 

<script> 

var x7 = function(){ 
    return "hello this is form function"; 
}; 

document.getElementById("demo").innerHTML = 

"x7: " + typeof x7 + "<br>"+ 
x7()+"<br>"; 
</script> 

</body> 
</html> 
1

這是因爲你串接功能,函數調用的不是結果。你有+x7+應該是+x7()+

1

你實際上沒有調用函數。如果你想輸出,你應該使用x7()