Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Questions regarding 'jQuery and AJAX' Office Example

I put most of the function into a variable to try and understand this better. I always screw up the html backticks, bear with me.

var url ="../data/employees.json";

var stuff = function(response) {
    var statusHTML = '<ul class = "bulleted">';
    $.each(response, function (index, employee) { 
      if (employee.inoffice === true) {
        statusHTML += '<li class = "in">';
      } else { 
        statusHTML += '<li class = "out">';
      }
      statusHTML += employee.name + '</li>';
    });
   statusHTML += '</ul>';
  $('#employeeList').html(statusHTML);
  }

$(document).ready(
$.getJSON(url, stuff));
  1. Is 'response' considered a keyword for .getJSON, like "this" or "var"?
  2. Around 5:00 in The Office Status Project Revisited video, Dave says that .each accepts two arguments, an array/object and a callback function. Is there a difference between a callback function and a regular function? Guess: does a callback function hinge on something like .getJSON to work?
  3. Why is the callback functions first argument called index? Can it be called something else? Can the second argument be something besides employee in regards to this example?

1 Answer

Dean Wagman
Dean Wagman
21,591 Points

Hi Michael,

  1. No. Response is not a reserved word. If you see on line 3 function(response) is written. 'response' is just a name we give the thing that is being returned to us. We could name it anything we want to. I believe 'response' was used here to make the code more abstract, meaning it reads less literal. But we could have named it 'message', or better yet 'employees' if we were to know that we would be receiving an array of employees from the server.
  2. Sort of. A Callback function is simply a function passed to another function. function inception. In most cases we will pass a function to another function, the main function will do some stuff, then call the function it was passed. For example the 'stuff' function expression (a function stored as a variable) is passed to the $.getJSON function. So the 'stuff' function is the callback to the $.getJSON function.
  3. Nope. We could have used any name here. 'Index' was used to make it more clear as to what that argument means. The $.each method can be used to iterate over each 'index' in an array, as well each property in an object. Most of the time you will see it named either "index, in, i," or "key, property, prop".

Hope that helped!

Dean

Sure did! Thank you.