缓存!saveLocations()和addLocation()函数有什么区别

Caches! What is the difference between the saveLocations() and addLocation() function?

本文关键字:什么 区别 函数 saveLocations 缓存 addLocation      更新时间:2023-09-26

我是Javascript的初学者,我被赋予了一项任务。它基本上是一个天气应用程序。。我创建了一个页面,将输出一个基于谷歌地理位置API的地方的纬度、经度和昵称。现在我想打电话给forecast.io,把结果告诉我。我应该做的是将纬度、经度和昵称存储在LocalStorage中。。。当点击"保存位置"按钮并将所有位置保存到列表中时,他们可以点击并获取天气信息。但后来我得到了一个骨架代码,我不知道它是做什么的。这两者有什么区别。AddLocation和我在后面写的savelocation()函数。我在这里写的唯一函数是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));
}

作为本单元的讲师,我建议Stack Overflow不是就作业提问的最佳场所。你的问题的答案需要从作业说明中获得知识,这些知识只有参加本单元的学生才能获得。

此外,您不应该公开发布任何代码(即您的问题解决方案)。作为提交作业的一部分,你签署一份声明,说这是你自己的工作,你没有与任何人分享你的工作。将代码发布到Stack Overflow会打破这种局面。不要这么做!

我建议你仔细阅读作业说明和作业常见问题解答。如果你还有问题,可以在单元论坛上提问,询问你的演示者,或者在咨询或桌面会议上提问。

为了回答您的问题,saveLocations()应该将LocationWeatherCache实例保存到本地存储中。addLocation()方法应该向LocationWeatherCache类的locations数组属性添加一个新位置,并且(正如HotGirlInYourRacDoingENG1003所说)它应该调用saveLocations()以确保此更改被持久化。

this.addLocationvar locations添加一个位置对象。它还应该调用saveLocations()来将这些更改保存到localStorage