Javascript 缓存!saveLocations()和addLocation()函数之间有什么区别?
我是Javascript的初学者,有人给了我一个任务。它基本上是一个天气应用程序。。 我创建了一个页面,该页面将基于谷歌地理定位API输出一个地方的纬度、经度和昵称。现在我想打电话给forecast.io,告诉我结果。 我要做的是将纬度、经度和昵称存储在本地存储中。。。单击“保存位置”按钮后,将所有位置保存到列表中,以便他们可以单击并获取天气信息。 但是我得到了一个框架代码,我不知道它是做什么的。 this.AddLocation和我在后面编写的savelocation()函数之间有什么区别。 我在这里编写的唯一函数是savelocation()函数,它将把位置保存到本地存储器中。其他函数是需要填充的骨架代码 任何关于类中的方法应该做什么的解释都会很有帮助 代码如下:Javascript 缓存!saveLocations()和addLocation()函数之间有什么区别?,javascript,caching,local-storage,forecasting,weather,Javascript,Caching,Local Storage,Forecasting,Weather,我是Javascript的初学者,有人给了我一个任务。它基本上是一个天气应用程序。。 我创建了一个页面,该页面将基于谷歌地理定位API输出一个地方的纬度、经度和昵称。现在我想打电话给forecast.io,告诉我结果。 我要做的是将纬度、经度和昵称存储在本地存储中。。。单击“保存位置”按钮后,将所有位置保存到列表中,以便他们可以单击并获取天气信息。 但是我得到了一个框架代码,我不知道它是做什么的。 this.AddLocation和我在后面编写的savelocation()函数之间有什么区别。
// 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));
}
this.addLocation
向var locations
添加位置对象。它还应该调用saveLocations()
将这些更改保存到localStorage
作为本单元的讲师,我建议堆栈溢出不是在作业中提问的最佳场所。你的问题的答案需要从作业指导中获得知识,而作业指导仅适用于学习本单元的学生
此外,您不应该公开发布任何代码(即问题的解决方案)。作为提交作业的一部分,你要签署一份声明,声明这是你自己的工作,你没有与任何人分享你的工作。将代码发布到堆栈溢出会打破这一点。别这样
我建议你仔细阅读作业说明和作业常见问题解答。如果您还有问题,请在单元论坛上提问,询问您的演示者,或在咨询或桌面会议上提问
在回答您的问题时,saveLocations()
应将LocationWeatherCache
实例保存到本地存储。addLocation()
方法应该向LocationWeatherCache
类的locations
数组属性添加一个新位置,并且(正如HotgirlinYourPracdoing1003所说)应该调用saveLocations()
以确保此更改被持久化