所以我都是JavaScript的新手,我們被要求使用NY-Times API創建Web項目。我的老師希望我使用構造函數對象模型來重寫這段代碼。所以我的問題是,如何做到這一點?構造函數對象模型JS
$.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);
}
}
);
也許看看這個MDN教程的面向對象的編程JS:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript –
謝謝您!我會這樣做 –