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

JavaScript

How the object json is passed to the getProfiles function inside the getJSON function?

In the very end of the video the instructor changes the event to:

btn.addEventListener('click', (event) => {
   getJSON(astrosUrl, getProfiles);
   event.target.remove;
})

The getProfiles function expects a parameter, that's not explicitly passed in this code, but the code works. How come?

This is really difficult to answer without knowing what course you're working on, or what your code looks like.

If you could either post a workspace snapshot using the camera icon in the top right, or post your code using the markdown below, that will help us determine what might be going on there.

```javascript

Your code here!

```

My guess would be that it's getting some json data from somewhere, but it's hard to say without more information.

1 Answer

In fact, you are calling the "getJSON" function, and it needs two parameters "(url, callback)":

function getJSON(url, callback) {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.onload = () => {
    if(xhr.status === 200) {
      let data = JSON.parse(xhr.responseText);
      return callback(data);
    }
  };
  xhr.send();
}

As the "url" parameter you are passing the value of the ''astrosURL" variable, and as the "callback" parameter your are passing the name of the function to be called (your are not calling it right now, your are just passing its name, so you don't need to pass any arguments to it).

Inside the getJSON function, in the line 'return callback(data);', the word "callback" is the parameter name to be substituted by the name of the function you passed before ("getProfiles"), so after susbtitution, that line of code would read return getProfiles(data);. And just until that moment you would be passing the 'data' parameter into the getProfiles function.