构造函数对象模型JS

时间:2016-11-23 10:39:40

标签: javascript api object-model

所以我都是JavaScript新手,我们被要求使用NY-Times API创建一个Web项目。我的老师要我使用构造函数对象模型重写此代码。所以我的问题是,如何做到这一点?

 $.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);
}
} 
);           

1 个答案:

答案 0 :(得分:1)

因为你已经创建了对象所以如果你想要一个新对象,你需要每次都写出它们的属性。现在这个函数(构造函数)将负责这一点。并且您可以尽可能多地创建对象。

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);