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

Hui Hui
Hui Hui
4,724 Points

JavaScript Class for API (getJSON) call

class callJSON{ constructor(url){ this.url = url; }

load(){
            // pass the this url to the getJSON
    $.getJSON(this.url, function(result){
        $.each(result, function(i, field){
            // store value from api to variable a
           var a = api.value;

        });
    })};

get loadresult(){
    // return the value of value a and make it able to get from the getter
     return this.load.a;
    }

}

const call1 = new callJSON("api.htm");

  1. How can I pass the value from new variable to this.url in this case?
  2. How can I get the value from the var a? when use console.log it return the full object or undefined.

Thanks in advance