0

Taken from w3 online tutorials:

$("button").click(function(){
    $("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr){
        if(statusTxt == "success")
            alert("External content loaded successfully!");
        if(statusTxt == "error")
            alert("Error: " + xhr.status + ": " + xhr.statusText);
    });
});

How does the function call know which parameters are passing, like say you only pass in statusTxt and xhr or you only want to pass in xhr?

Please go easy on me guys :) this is my first question ever

2
  • 2
    You go in order. So you use the first parameter, you get the first one they passed, check my answer below for more help. Commented Aug 12, 2016 at 21:45
  • Since this is a callback function, you don't get to decide which parameters are being passed. They're passed to your function by .load(), and it always passes all of them. You can ignore the ones you don't care about, though. Commented Aug 12, 2016 at 22:00

4 Answers 4

1

It goes in order of what was passed to it.

Take the example below,

function test(callback) {
  var x = 1,
      y = 2,
      z = 3;

  if (typeof callback == 'function')
    callback(x, y, z);
}

test(function (z, y, x) {
  console.log(z);
});

The output would be,

1

Because in the original code, I passed x which equals 1 first to the callback. It doesn't matter what I named it. The functions passed to it are known as callback functions.

Note

Just to explain the typeof check, I'm checking to see if the passed variable is actually a function, though in this test it is going to be a function, when you use callbacks it is always good to ensure you validate it.

Sign up to request clarification or add additional context in comments.

Comments

1

It does it based on parameter order, and you can't just pass in the second or third parameters by themselves.

In your example, if you pass in one parameter, it's going to be responseTxt, two parameters will be responseTxt and statusTxt. If you want to pass just statusTxt you would have to pass in a null for responseTxt and then your value for statusTxt.

3 Comments

this is a perfect answer and completely steers me away from thinking it had to do with matching exact variable names XD THANK YOU
Note that there are many jQuery functions where you can skip arguments. jQuery figures out which arguments you passed by checking the data type.
And for that to work, there has to be a distinct pattern of number and type of parameters, kind of like function/method overloading in other languages.
1

Simply, it doesn't, because it assigns each parameter you pass on from your fnction call from left to right. Let's say you have this function, copied from you:

function someFunction(responseTxt, statusTxt, xhr){
    if(statusTxt == "success")
        alert("External content loaded successfully!");
    if(statusTxt == "error")
        alert("Error: " + xhr.status + ": " + xhr.statusText);
}

and you want to pass values for variables statusTxt: "success" and xhr: your_xhr when you call this function. If you called the function like this:

someFunction("success", your_xhr);

this means "success" will be assigned to the parameter responseTxt and your_xhr to statusTxt.

Normally, you could just set responseTxt to null and set the rest to what you want, for example:

someFunction(null, "success", your_xhr);

or use an object literal in your function definition. Here's an example:

function someFunction(options){
    if(options.statusTxt == "success")
        alert("External content loaded successfully!");
    if(options.statusTxt == "error")
        alert("Error: " + options.xhr.status + ": " + options.xhr.statusText);
}

and your function call would look like this:

someFunction({statusTxt: "success", xhr: your_xhr});

Of course you need to check if responseTxt is undefined in case you don't use it.

Hope this helped.

Comments

-1

This is called callbacks. You dont know for sure that's why you read documentation before using anything. Callbacks can send no response. Or can send a lot of data back. In your case from docs http://api.jquery.com/load/ we have 3 parameters that will be returned. But each library have it's own docs and it's own return results. Hope this helps.

2 Comments

This has nothing to do with the question, which is about how optional arguments are processed.
How does the function call know which parameters are passing, like say you only pass in statusTxt and xhr or you only want to pass in xhr? from question? WHAT? statusTxt and xhr is callbacks from load method. And function knows because it's returning results in callback... OMG..Read the docs guys!Maybe i'm not undestand question correctly, but from question hes talking about response parameters not parameters that are passed to load function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.