javascriptapiobject-model

Constructor object model JS


So I'm all new to JavaScript and for a school assignment we were asked to use the NY-Times API to create a web-project. My teacher wants me to rewrite this code, using a constructor object model. So my question is, how can this be done?

 $.getJSON('https://api.nytimes.com/svc/events/v2/listings.json', 
            {'api-key': "2cb941103998461990415c7e7fafb8c7",  'filters': "-movies"
            },
            function(data) { 
    for (var i = 0; i < data.results.length; i++) { 
      var event= data.results[i];


var nameEvent= event.event_name; 
var Category3= event.category
var descriptionWeb= event.web_description
var Borough3 = event.borough;
var timeAndTime = event.date_time_description;

var eventPost = {
    eventName: nameEvent,
    category: Category3,
    description: descriptionWeb,
    borough: Borough3,
    time: timeAndTime
    };

var eventHTML= $( '<event>' 
+ '  <h2>'+ nameEvent  + '</h2>'
+ '  <h3>'+ Category3 + '</h3>' 
+ '  <p>' + descriptionWeb + '</p>' 
+ '  <p>' + Borough3 + '</p>' 
+ '  <p>' + timeAndTime + '</p>'
+ '</event>' );

var events= $('#EVENTS'); 
events.append(eventHTML);
}
} 
);           

Solution

  • since you have created object so if you want a new object you need to write their properties each time . now this function(constructor) will take care of that . and you can create object as many as possible.

    function model(a,b,c,d,e) {
      this.eventName = a;
      this.category = b;
      this.description = c;
      this.borough = d;
      this.time = e;
    }
    
    
    var eventPost =new model (nameEvent,Category3,descriptionWeb,Borough3,timeAndTime);