Is it possible to call a javascript function without paranthesis? (). In the below code, from a book, has the line,
http.onreadystatechange = useHttpResponse;
If there is no paramenters in the function definition, can we call without 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 = "";
}
}
newkeyword. Or you use something hacky likesetTimeout(func, 0), but that's not really reasonable IMO. edit: As Niet said, in this case you are not calling the function and you don't want to. You are assigning a function as callback so that other code can call the function later.http.onreadystatechange = useHttpResponseis a reference touseHttpResponsefunction. You can do it partly because functions are first class objects in JavaScript. You can assign a function to a variable:var square = function(x) { return x*x; }. You can even have an array of functions:var fs = [square, function(x) { return x*x*x; }]You might find it interesting that yes you can invoke a function with a syntax like this:square.apply(null, [5]). Notice how I'm callingapplyfunction onsquarefunction.