0

I often do not understand how the value of certain function parameters is determined. Take the following code snippet for example:

function gotItAll(res) {

$('#actionscreen').modal('hide')

if (res.ErrorHtml != "" && res.ErrorHtml.substring(0,31) != "Error copying to sub data table") {
    
    document.getElementById('GridView').innerHTML = res.ErrorHtml;
    InfoMessage("Error", res.ErrorHtml);
} else {

    if (res.BrowseTreeBreadCrumbs != null) {
        //generateBrowseTreeHtml(res);
        sessionStorage.setItem("browseTreeBC", res.BrowseTreeBreadCrumbs);
        if (res.BrowseTreeBreadCrumbs.indexOf("-1") > 0 || res.BrowseTreeBreadCrumbs.indexOf("- -1") > 0 || res.BrowseTreeBreadCrumbs.indexOf("- -999") > 0 || res.BrowseTreeBreadCrumbs.indexOf("-999") > 0 || res.BrowseTreeBreadCrumbs.indexOf("-999") > 0 || res.BrowseTreeBreadCrumbs.indexOf("</b>  </span") > 0) {
            $('#breadCrumbsID').hide();
        }
    }
    
    BaseTable = res.TableName; 
    recs = res.records;

'res' is a parameter that I could not find defined anywhere, yet it returns a value. I was not able to identify where gotItAll() is called and passed a value other than res. In addition, we have res.ErrorHtml, res.BrowseTreeBreadCrumbs.indexOf, res.TableName etc. that do not appear to be defined anywhere, yet they all return different values.

NickyLarson
  • 139
  • 8
  • res might be defined in a script imported in global scope. Check you index.html for included scripts. – Shubham Chaudhary Mar 20 '19 at 10:01
  • 1
    It sounds like `res` is a response from possibly an AJAX call or a promise – I'd double check if the serverside / API is actually returning all this data, which is why it's not easy to find anywhere else. – Djave Mar 20 '19 at 10:07
  • Yes, you are right, it is returning data from a server. Does that mean that it would be defined in a particular library? is there a way to locate the res object? – NickyLarson Mar 20 '19 at 10:33
  • As described by gbalduzzi, ‘gotItAll’ is passed as a parameter to ‘addNew’. Inside ‘addNew’ the function ‘gotItAll’ is invoked with some object as parameter. (Likely a result from an Ajax-call of Some sort) That object may carry any name at that point. However, once inside the context of ‘gotItAll’ that same object passed as a parameter becomes known as ‘res’ because that’s how its defined in the signature of the ‘gotItAll’ function – Geert-Jan Mar 20 '19 at 11:00
  • Please don't vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed, and thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](/help/what-to-do-instead-of-deleting-question) – cigien Feb 04 '21 at 09:50

2 Answers2

0

res is passed in from whatever calls gotItAll

The res.whatever are the attributes of the res object

The name res is just for use in that function, and will likely have a different name to what was used as the parameter.

Example

funciton addOneToNumber(numberToAddTo)
{
   return numberToAddTo += 1;
}

function mainFunction()
{
   var newNumber = addOneToNumber(1)
}

Edit:

You can use any object in javascript to be used as a parameter. In this case the function has been used. It also looks like failedCallback is also a function. Which would be called if the call to the server fails

Inside InventoryEditor.ItemEdit.AddNew somewhere, gotItAll is called with res which would of been a result of the server call from whichever type of call is made

Beshman88
  • 63
  • 1
  • 1
  • 8
  • Thanks, that makes sense. But in this particular case, gotItAll is itself passed as a parameter: InventoryEditor.ItemEdit.AddNew(itemTypeUid, itemTypeName, gotItAll, failedCallBack . It is not called like in the above example where a clear value is given to res (addOneToNumber(1)). I'm still struggling to figure out how to find the res object. – NickyLarson Mar 20 '19 at 10:20
  • Updated the answer – Beshman88 Mar 20 '19 at 10:42
0

The code snippet you posted is just the function declaration. Basically that snipper defines the existence of a function called gotItAll, which takes one parameter as the input. The parameter is called res, so inside the function, the word res refers to the parameter of the function.

Let's use a simple function as example:

function double(x) {
  return x * 2
}

This function as a parameter as well, called x. Inside the function, you refer to it as x.

When you want to use the function, you call the function and you gave a parameter, such as:

double(2) // returns 4
double(4) // returns 8
double(7) // returns 14

As you can see, we can pass to the function double different values. When you call double(4), basically the function considers x = 4, and so everytime inside the function declaration you use x, you will be using the value 4 instead.

You can also pass different variables to the function:

var y = 10
double(y) // returns 20

Same as before, you give to the function the variable y, whose value is 10. So basically the program does x = y and everytime it refers to x inside the function declaration, it will be using the value of y.

Back to your example. I don't know what that function do, but basically res is the placeholder name of the parameter that will be passed to the function. That function can be called from around the code as gotItAll(variableName), and the function will be executed considering res = variableName. In the snipper you pasted, the function is only declared but there are not examples of actual usages of the function, that's why you can't see where res comes from.

EDIT: after your comment, I realized what you are actually referring to. Actually, in Javascript you can pass functions as the parameter of another function. Take for example the double function declared above. Now I want to create a function that doubles the number only if they are even. I can declare this:

function doubleEven(x, doublingFunction) {
  if (x % 2 === 0) {
    return doublingFunction(x)
  } else {
    return x
  }
}

and I can use it as:

doubleEven(2, double) // returns 4
doubleEven(5, double) // returns 5

Basically, I pass double as a parameter to the doubleEven function. So, inside the doubleEven declaration, doublingFunction becomes equal to double and so it is a function.

Of course in this example this is silly, there is no need to pass a function for doubling a number. However this is used a lot in javascript to provide a callback to asynchronous task, such as a network request.

gbalduzzi
  • 9,356
  • 28
  • 58
  • Thanks, I understand the above examples of parameter functions. In this case though, at no point is gotItAll() called in the code, so, as far as I can tell, the placeholder is never replaced with a specific value, such as gotItAll(3) for example. Instead gotItAll is passed as a parameter itself like this: InventoryEditor.ItemEdit.AddNew(itemTypeUid, itemTypeName, gotItAll, failedCallBack); – NickyLarson Mar 20 '19 at 10:27
  • Now I see what you are referring to! I edited my answer – gbalduzzi Mar 20 '19 at 10:48