JavaScript AJAX Basics (retiring) Programming AJAX Stage 2 Challenge

Aaron Coursolle
Aaron Coursolle
18,012 Points

What does "anonymous function" mean when in the context of the console?

In a previous workspace, clicking the down arrow beside "XHR finished loading: GET..." showed the array. Now, in this lesson, when you click the down arrow you get "(anonymous function) @ widget.js:42". This is true for the teacher's code, as well as mine, except his gives the line 20 instead of 42.

The teacher's portion is var = xhr, mine is var = xyy.

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
  if(xhr.readyState === 4 && xhr.status === 200) {
    var employees = JSON.parse(xhr.responseText);
    var statusHTML = '<ul class="bulleted">';
    for (var i=0; i<employees.length; i += 1) {
      if (employees[i].inoffice === true) {
        statusHTML += '<li class="in">';
      } else {
        statusHTML += '<li class="out">';
      }
      statusHTML += employees[i].name;
      statusHTML += '</li>';
    }
    statusHTML += '</ul>';
    document.getElementById('employeeList').innerHTML = statusHTML;
  }
};
xhr.open('GET', '../data/employees.json');
xhr.send();


var xyy = new XMLHttpRequest();
xyy.onreadystatechange = function () {
  if(xyy.readyState === 4 && xyy.status === 200) {
  var boardrooms = JSON.parse(xyy.responseText);
  var availableHTML = '<ul class="rooms">';
  for (var i=0; i<boardrooms.length; i += 1) {
    if(boardrooms[i].available === false) {
      availableHTML += '<li class="full">';
    } else {
      availableHTML += '<li class="empty">';
    }
    availableHTML +=  boardrooms[i].room;
    availableHTML += '</li>';
    }
    availableHTML +='</ul>';
    document.getElementById('roomList').innerHTML = availableHTML;
  }
};
xyy.open('GET', '../data/rooms.json');
xyy.send();
Aaron Coursolle
Aaron Coursolle
18,012 Points

Why don't I see the array, in the console?

Tommy May
Tommy May
12,056 Points

Hey Aaron,

In the code you posted there is no console.log() functions therefor nothing is being printed to the console. If you want to see the array of objects the JSON is giving you then you can do console.log(boardrooms) right after var boardrooms = JSON.parse(xyy.responseText);

In general anonymous function means a function without a name.

xhr.onreadystatechange = function ()

A named function would be something like

var myFunction = function () {
  function stuff
}

xhr.onreadystatechange = myFunction()

I hope this helps.