我是Javascript的初學者,我被賦予了一項任務。這基本上是一個天氣應用程序.. 我創建了一個頁面,將輸出基於谷歌地理定位API的地方的經度,緯度和綽號。現在我想打電話給forecast.io,讓我知道結果。 我應該做的是將經緯度和暱稱存儲在LocalStorage中......點擊「保存位置」按鈕並將所有位置保存到列表中,以便點擊並獲取天氣信息。 但後來我得到了一個骨架代碼,我不知道它的功能。 我在後面寫的this.AddLocation和savelocation()函數之間有什麼區別。 我在這裏寫的唯一功能是將位置保存到本地存儲的savelocation()函數。其他函數是需要填充的骨架代碼。緩存! saveLocations()和addLocation()函數有什麼區別?
任何關於這個方法應該做什麼的解釋都會有很大的幫助!
的代碼如下:
// Returns a date in the format "YYYY-MM-DD".
Date.prototype.simpleDateString = function() {
function pad(value)
{
return ("0" + value).slice(-2);
}
var dateString = this.getFullYear() + "-" +
pad(this.getMonth() + 1, 2) + '-' +
pad(this.getDate(), 2);
return dateString;
}
// Date format required by forecast.io API.
// We always represent a date with a time of midday,
// so our choice of day isn't susceptible to time zone errors.
Date.prototype.forecastDateString = function() {
return this.simpleDateString() + "T12:00:00";
}
// Code for LocationWeatherCache class and other shared code.
// Prefix to use for Local Storage. You may change this.
var APP_PREFIX = "weatherApp";
function LocationWeatherCache()
{
// Private attributes:
var locations = [];
var callbacks = {};
// Public methods:
// Returns the number of locations stored in the cache.
//
this.length = function() {
};
// Returns the location object for a given index.
// Indexes begin at zero.
//
this.locationAtIndex = function(index) {
};
// Given a latitude, longitude and nickname, this method saves a
// new location into the cache. It will have an empty 'forecasts'
// property. Returns the index of the added location.
//
this.addLocation = function(latitude, longitude, nickname)
{
}
// Removes the saved location at the given index.
//
this.removeLocationAtIndex = function(index)
{
}
// This method is used by JSON.stringify() to serialise this class.
// Note that the callbacks attribute is only meaningful while there
// are active web service requests and so doesn't need to be saved.
//
this.toJSON = function() {
};
// Given a public-data-only version of the class (such as from
// local storage), this method will initialise the current
// instance to match that version.
//
this.initialiseFromPDO = function(locationWeatherCachePDO) {
};
// Request weather for the location at the given index for the
// specified date. 'date' should be JavaScript Date instance.
//
// This method doesn't return anything, but rather calls the
// callback function when the weather object is available. This
// might be immediately or after some indeterminate amount of time.
// The callback function should have two parameters. The first
// will be the index of the location and the second will be the
// weather object for that location.
//
this.getWeatherAtIndexForDate = function(index, date, callback) {
};
// This is a callback function passed to forecast.io API calls.
// This will be called via JSONP when the API call is loaded.
//
// This should invoke the recorded callback function for that
// weather request.
//
this.weatherResponse = function(response) {
};
// Private methods:
// Given a latitude and longitude, this method looks through all
// the stored locations and returns the index of the location with
// matching latitude and longitude if one exists, otherwise it
// returns -1.
//
function indexForLocation(latitude, longitude)
{
}
}
// Restore the singleton locationWeatherCache from Local Storage.
//
function loadLocations()
{
}
// Save the singleton locationWeatherCache to Local Storage.
//
function saveLocations(nickname,latitude,longtitude){
var locations = JSON.parse(localStorage.getItem('APP_PREFIX')) || [];
locations.push({nickname: nickname, latitude: latitude, longtitude:longtitude});
localStorage.setItem('APP_PREFIX', JSON.stringify(locations));
}