![](/img/trans.png)
[英]How to query the arguments of a javascript function without calling it
[英]Calling a javascript function without arguments
是否可以在沒有括號的情況下撥打 javascript function? (). 在下面的代碼中,來自一本書,有一行,
http.onreadystatechange = useHttpResponse;
如果function定義中沒有參數,我們可以不調用arguments嗎?
function getServerText() {
var myurl = 'ajax.php';
myRand = parseInt(Math.random() * 999999999999999);
var modurl = myurl + "?rand=" + myRand;
http.open("GET", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
function useHttpResponse() {
if (http.readyState == 4) {
if (http.status == 200) {
var mytext = http.responseText;
document.getElementById('myPageElement')
.innerHTML = mytext;
}
} else {
document.getElementById('myPageElement')
.innerHTML = "";
}
}
分配事件處理程序時,您不想調用該函數,而是給它提供了對函數的引用,稍后將對其進行調用。
所以不行。 ()
用於表示“調用此函數”,在這種情況下,沒有參數。
這是相同的方式:
function add2values(a, b) { return a + b } const objX = { name: 'xyz', myAdd: null } objX.myAdd = add2values console.log( objX.myAdd(1, 2) ) // -> 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.